File size: 3,034 Bytes
266e0c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d571a4
26c48be
266e0c4
26c48be
 
 
 
 
4d571a4
ee89f5b
4d571a4
26c48be
ee89f5b
266e0c4
26c48be
 
 
 
 
 
 
ee0b430
26c48be
 
 
 
 
 
 
4d571a4
26c48be
266e0c4
 
 
a8460ac
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
94
95
96
97
98
99
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 

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