Spaces:
Sleeping
Sleeping
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. | |
Your job is to generate a clean, formatted schedule for a specific day. | |
Constraints: | |
- Do NOT return ANY internal reasoning, explanations, or commentary. | |
- Do NOT return any Markdown, <tags>, or extra newlines. | |
- Do NOT start with any greeting or introduction. | |
- You MUST return exactly and only the schedule lines in this format: | |
HH:MM - HH:MM : ProgramName | |
Example: | |
08:00 - 08:30 : Morning Match Recap | |
Instructions: | |
- Avoid back-to-back programs of the same ProgramType | |
- Prioritize highest-rated programs during prime time (18:00–22:00) | |
- Do not repeat any program on the same day | |
- Use the full 4-hour prime time wisely | |
- Only return the final formatted lines in the structure above | |
- Rating must be a number with one decimal and enclosed in parentheses | |
Now generate a Saturday schedule using the following available programs: | |
- Morning Match Recap (Digest, 30 mins, 6.2) | |
- Champions League Highlights (Highlights, 60 mins, 8.2) | |
- FanZone Live (TalkShow, 60 mins, 7.8) | |
- Classic Replays (Replay, 90 mins, 7.4) | |
- Legends Talk (TalkShow, 30 mins, 6.9) | |
Start the schedule from 09:00 AM and use prime time from 18:00 to 22:00 wisely. | |
""" | |
prompt = PromptTemplate.from_template(template) | |
llm = ChatGroq(model_name = "qwen/qwen3-32b", api_key = os.environ["GROQ_API_KEY"]) | |
chain = prompt | llm | |
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) | |