Spaces:
Running
Running
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")
|
|
@@ -44,7 +79,7 @@ app.add_middleware(
|
|
| 44 |
|
| 45 |
@app.get("/favicon.ico")
|
| 46 |
async def favicon():
|
| 47 |
-
return HTMLResponse("") # or serve a real favicon
|
| 48 |
|
| 49 |
|
| 50 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
@@ -124,9 +159,16 @@ async def load_chat(request: Request, id: str):
|
|
| 124 |
# Route to save chat history
|
| 125 |
@app.post("/hist/")
|
| 126 |
async def save_chat_history(history: dict):
|
| 127 |
-
#
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
@app.post("/webhook")
|
| 131 |
async def receive_form_data(request: Request):
|
| 132 |
form_data = await request.json()
|
|
@@ -155,6 +197,3 @@ async def chat(request: MessageRequest):
|
|
| 155 |
@app.get("/")
|
| 156 |
def read_root():
|
| 157 |
return {"message": "Welcome to the API"}
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
| 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=os.getenv("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")
|
|
|
|
| 79 |
|
| 80 |
@app.get("/favicon.ico")
|
| 81 |
async def favicon():
|
| 82 |
+
return HTMLResponse("") # or serve a real favicon if you have one
|
| 83 |
|
| 84 |
|
| 85 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
| 159 |
# Route to save chat history
|
| 160 |
@app.post("/hist/")
|
| 161 |
async def save_chat_history(history: dict):
|
| 162 |
+
# Check if 'history' is a key in the incoming dictionary
|
| 163 |
+
user_id = history.get('userId')
|
| 164 |
+
print(user_id)
|
| 165 |
+
if 'history' in history and isinstance(history['history'], list):
|
| 166 |
+
print("Received history:", history['history']) # Debugging line
|
| 167 |
+
cleaned_summary = summarize_conversation(llm_client, history['history'])
|
| 168 |
+
print("Cleaned summary:", cleaned_summary) # Debugging line
|
| 169 |
+
return {"summary": cleaned_summary, "message": "Chat history saved"}
|
| 170 |
+
else:
|
| 171 |
+
return JSONResponse(status_code=400, content={"message": "Invalid history format"})
|
| 172 |
@app.post("/webhook")
|
| 173 |
async def receive_form_data(request: Request):
|
| 174 |
form_data = await request.json()
|
|
|
|
| 197 |
@app.get("/")
|
| 198 |
def read_root():
|
| 199 |
return {"message": "Welcome to the API"}
|
|
|
|
|
|
|
|
|