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