File size: 2,687 Bytes
266e0c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d571a4
ee0b430
266e0c4
ee0b430
 
 
4d571a4
ee0b430
 
 
 
4d571a4
ee0b430
266e0c4
 
ee0b430
 
 
 
4d571a4
ee0b430
 
 
 
 
266e0c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
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 day/time context, generate an optimal TV schedule.

πŸ—“οΈ Context:
- Day: {day_of_week}
- Is Holiday: {is_holiday}

πŸ“‹ Constraints:
- No back-to-back programs of the same `ProgramType`
- Prioritize highest-rated programs during prime time (18:00–22:00)
- No program can be repeated on the same day

πŸŽ₯ Available Programs:
{program_list}

πŸ“ OUTPUT FORMAT:
Return ONLY the final schedule in this format:

HH:MM - HH:MM ProgramName (ProgramType, Rating): <1-line reason>

⚠️ VERY IMPORTANT:
- Do NOT include any explanation, notes, or internal thinking
- Do NOT use tags like <think>, <plan>, <analyze>
- Do NOT return anything except the final schedule in the format provided above
- The output MUST start directly with the first time slot
"""

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)