yashxx07 commited on
Commit
74ed26d
·
verified ·
1 Parent(s): 63f5def

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +61 -10
main.py CHANGED
@@ -6,6 +6,7 @@ from pydantic import BaseModel
6
  import re
7
  import replicate
8
  import os
 
9
  from supabase import create_client, Client
10
 
11
  url: str = os.environ.get("DB_URL")
@@ -66,6 +67,32 @@ async def root(item: Item):
66
 
67
 
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  @app.post("/summarize-v2")
70
  async def root(item: Item):
71
  try:
@@ -74,18 +101,42 @@ async def root(item: Item):
74
  if len(article) == 0:
75
  return {'summary': ""}
76
 
77
- def event_generator():
78
- for event in replicate.stream("snowflake/snowflake-arctic-instruct", input={
79
- "prompt": f"summarize this news article in {item.max_tokens} lines:" + article,
80
- "temperature": 0.2,
81
- "max_new_tokens" : 1000
82
- }):
83
- # Yield the event as a string
84
- yield str(event)
85
- print(str(event), end="")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
  # Use StreamingResponse to stream the events
88
- return StreamingResponse(event_generator(), media_type='text/event-stream')
89
 
90
  except requests.RequestException as e:
91
  return {"error": str(e), "status_code": 500}
 
6
  import re
7
  import replicate
8
  import os
9
+ import json
10
  from supabase import create_client, Client
11
 
12
  url: str = os.environ.get("DB_URL")
 
67
 
68
 
69
 
70
+ # @app.post("/summarize-v2")
71
+ # async def root(item: Item):
72
+ # try:
73
+ # article = extract_article_content(item.url)
74
+
75
+ # if len(article) == 0:
76
+ # return {'summary': ""}
77
+
78
+ # def event_generator():
79
+ # for event in replicate.stream("snowflake/snowflake-arctic-instruct", input={
80
+ # "prompt": f"summarize this news article in {item.max_tokens} lines:" + article,
81
+ # "temperature": 0.2,
82
+ # "max_new_tokens" : 1000
83
+ # }):
84
+ # # Yield the event as a string
85
+ # yield str(event)
86
+ # #print(str(event), end="")
87
+
88
+ # # Use StreamingResponse to stream the events
89
+ # return StreamingResponse(event_generator(), media_type='text/event-stream')
90
+
91
+ # except requests.RequestException as e:
92
+ # return {"error": str(e), "status_code": 500}
93
+
94
+
95
+
96
  @app.post("/summarize-v2")
97
  async def root(item: Item):
98
  try:
 
101
  if len(article) == 0:
102
  return {'summary': ""}
103
 
104
+
105
+ url = 'https://yashxx07-hf-llm-api.hf.space/api/v1/chat/completions'
106
+
107
+ headers = { "content-type": "application/json" }
108
+ data = {
109
+ "model": "mixtral-8x7b",
110
+ "messages": [
111
+ {
112
+ "role": "user",
113
+ "content": "who are you?"
114
+ }
115
+ ],
116
+ "temperature": 0.5,
117
+ "top_p": 0.95,
118
+ "max_tokens": -1,
119
+ "use_cache": False,
120
+ "stream": True
121
+ }
122
+
123
+ def get_stream(url):
124
+ s = requests.Session()
125
+
126
+ with s.post(url, headers=headers, stream=True, json=data ) as resp:
127
+
128
+ for line in resp.iter_lines():
129
+ if line:
130
+ obj = json.loads(line[5:])
131
+ try:
132
+ ouput = obj["choices"][0]["delta"]["content"])
133
+ #print(obj["choices"][0]["delta"]["content"])
134
+ yield str(ouput)
135
+ except:
136
+ pass
137
 
138
  # Use StreamingResponse to stream the events
139
+ return StreamingResponse(get_stream(), media_type='text/event-stream')
140
 
141
  except requests.RequestException as e:
142
  return {"error": str(e), "status_code": 500}