Update app.py
Browse files
app.py
CHANGED
@@ -12,11 +12,46 @@ import uuid # for generating unique IDs
|
|
12 |
import datetime
|
13 |
from fastapi.middleware.cors import CORSMiddleware
|
14 |
from fastapi.templating import Jinja2Templates
|
|
|
|
|
|
|
15 |
|
16 |
|
17 |
# Define Pydantic model for incoming request body
|
18 |
class MessageRequest(BaseModel):
|
19 |
message: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
|
22 |
os.environ["HF_TOKEN"] = os.getenv("HF_TOKEN")
|
@@ -126,6 +161,8 @@ async def load_chat(request: Request, id: str):
|
|
126 |
async def save_chat_history(history: dict):
|
127 |
# Logic to save chat history, using the `id` from the frontend
|
128 |
print(history) # You can replace this with actual save logic
|
|
|
|
|
129 |
return {"message": "Chat history saved"}
|
130 |
@app.post("/webhook")
|
131 |
async def receive_form_data(request: Request):
|
|
|
12 |
import datetime
|
13 |
from fastapi.middleware.cors import CORSMiddleware
|
14 |
from fastapi.templating import Jinja2Templates
|
15 |
+
from huggingface_hub import InferenceClient
|
16 |
+
import json
|
17 |
+
import re
|
18 |
|
19 |
|
20 |
# Define Pydantic model for incoming request body
|
21 |
class MessageRequest(BaseModel):
|
22 |
message: str
|
23 |
+
repo_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
24 |
+
llm_client = InferenceClient(
|
25 |
+
model=repo_id,
|
26 |
+
token=userdata.get('HF_TOKEN'),
|
27 |
+
)
|
28 |
+
def summarize_conversation(inference_client: InferenceClient, history: list):
|
29 |
+
# Construct the full prompt with history
|
30 |
+
history_text = "\n".join([f"{entry['sender']}: {entry['message']}" for entry in history])
|
31 |
+
full_prompt = f"{history_text}\n\nSummarize the conversation in three concise points only give me only Summarization in python list formate :\n"
|
32 |
+
|
33 |
+
response = inference_client.post(
|
34 |
+
json={
|
35 |
+
"inputs": full_prompt,
|
36 |
+
"parameters": {"max_new_tokens": 512},
|
37 |
+
"task": "text-generation",
|
38 |
+
},
|
39 |
+
)
|
40 |
+
|
41 |
+
# Decode the response
|
42 |
+
generated_text = json.loads(response.decode())[0]["generated_text"]
|
43 |
+
|
44 |
+
# Use regex to extract the list inside brackets
|
45 |
+
matches = re.findall(r'\[(.*?)\]', generated_text)
|
46 |
+
|
47 |
+
# If matches found, extract the content
|
48 |
+
if matches:
|
49 |
+
# Assuming we only want the first match, split by commas and strip whitespace
|
50 |
+
list_items = matches[0].split(',')
|
51 |
+
cleaned_list = [item.strip() for item in list_items]
|
52 |
+
return cleaned_list
|
53 |
+
else:
|
54 |
+
return generated_text
|
55 |
|
56 |
|
57 |
os.environ["HF_TOKEN"] = os.getenv("HF_TOKEN")
|
|
|
161 |
async def save_chat_history(history: dict):
|
162 |
# Logic to save chat history, using the `id` from the frontend
|
163 |
print(history) # You can replace this with actual save logic
|
164 |
+
cleaned_summary = summarize_conversation(llm_client, history)
|
165 |
+
print(cleaned_summary)
|
166 |
return {"message": "Chat history saved"}
|
167 |
@app.post("/webhook")
|
168 |
async def receive_form_data(request: Request):
|