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. | |
| 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 | |
| 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) | |