Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +84 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_core.prompts import PromptTemplate
|
2 |
+
from langchain_groq import ChatGroq
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from fastapi import FastAPI, Response
|
5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
6 |
+
from langchain_core.output_parsers import StrOutputParser
|
7 |
+
import pandas as pd
|
8 |
+
import uvicorn
|
9 |
+
import re
|
10 |
+
import os
|
11 |
+
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
app = FastAPI()
|
15 |
+
|
16 |
+
origins = ["*"]
|
17 |
+
|
18 |
+
app.add_middleware(
|
19 |
+
CORSMiddleware,
|
20 |
+
allow_origins=origins,
|
21 |
+
allow_credentials=True,
|
22 |
+
allow_methods=["*"],
|
23 |
+
allow_headers=["*"]
|
24 |
+
)
|
25 |
+
|
26 |
+
os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
|
27 |
+
|
28 |
+
program_df = pd.DataFrame({
|
29 |
+
"ProgramTitle": ["Champions League Highlights", "Legends Talk", "Classic Replays", "Morning Match Recap", "FanZone Live"],
|
30 |
+
"ProgramType": ["Highlights", "TalkShow", "Replay", "Digest", "TalkShow"],
|
31 |
+
"Duration": [60, 30, 90, 30, 60],
|
32 |
+
"TargetAudience": ["Males 18-35", "Females 25-40", "General", "All", "Males 18-50"],
|
33 |
+
"AvgRating": [8.2, 6.9, 7.4, 6.2, 7.8]
|
34 |
+
})
|
35 |
+
|
36 |
+
template = """
|
37 |
+
You are a sports TV schedule assistant.
|
38 |
+
Given the available program data and current date/time constraints,
|
39 |
+
suggest an optimal schedule plan.
|
40 |
+
|
41 |
+
Today's context:
|
42 |
+
- Day: {day_of_week}
|
43 |
+
- Is Holiday: {is_holiday}
|
44 |
+
|
45 |
+
Constraints:
|
46 |
+
- Avoid repeating the same ProgramType back-to-back
|
47 |
+
- Prioritize high-rated programs in prime time (18:00–22:00)
|
48 |
+
- Avoid replays more than once a day
|
49 |
+
|
50 |
+
Available Programs:
|
51 |
+
{program_list}
|
52 |
+
|
53 |
+
Suggest a schedule for the day (in HH:MM format), with reasoning in not more than 2 lines.
|
54 |
+
Give result as final answer **ONLY**, no thinking needed.
|
55 |
+
"""
|
56 |
+
|
57 |
+
prompt = PromptTemplate.from_template(template)
|
58 |
+
llm = ChatGroq(model_name = "qwen-qwq-32b", api_key = os.environ["GROQ_API_KEY"])
|
59 |
+
|
60 |
+
chain = prompt | llm
|
61 |
+
|
62 |
+
@app.get("/api/v1/get_dynamic_schedule")
|
63 |
+
def get_dynamic_schedule():
|
64 |
+
try:
|
65 |
+
response = chain.invoke({
|
66 |
+
"day_of_week": "Saturday",
|
67 |
+
"is_holiday": "Yes",
|
68 |
+
"program_list": program_df.to_string(index=False)
|
69 |
+
})
|
70 |
+
|
71 |
+
text_data = response.content
|
72 |
+
if text_data and "</think>" in text_data:
|
73 |
+
result = re.split(r'</think>', text_data, maxsplit=1)[-1].strip()
|
74 |
+
return result
|
75 |
+
|
76 |
+
return response
|
77 |
+
|
78 |
+
except Exception as e:
|
79 |
+
return Response(content=str(e), media_type="text/markdown")
|
80 |
+
|
81 |
+
if __name__ == "__main__":
|
82 |
+
#get_dynamic_schedule()
|
83 |
+
uvicorn.run(app, host= "127.0.0.1", port= 8000)
|
84 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
langchain-groq
|
4 |
+
python-dotenv
|
5 |
+
langchain
|
6 |
+
langchain_community
|
7 |
+
pandas
|