Fasty commited on
Commit
c097840
·
verified ·
1 Parent(s): b1c2033

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from groq import Groq
4
+
5
+ # Initialize Groq client
6
+ api_key = os.getenv("GROQ_API_KEY")
7
+ client = Groq(api_key=api_key)
8
+
9
+ # Initialize conversation history
10
+ conversation_history = []
11
+
12
+ def chat_with_bot_stream(user_input):
13
+ global conversation_history
14
+ conversation_history.append({"role": "user", "content": user_input})
15
+
16
+ if len(conversation_history) == 1:
17
+ conversation_history.insert(0, {
18
+ "role": "system",
19
+ "content": "You are a music and genre recommendation bot designed to help users discover new music based on their preferences, mood, or activity.\n\nYour responses should be engaging, personalized, and insightful. You can recommend:\n\n-Specific songs, albums, or artists\n-Genres based on mood, activity, or past preferences\n-Hidden gems and deep cuts for music enthusiasts\n-Trending or classic hits based on user taste\n\nBe conversational, suggest multiple options when possible, and encourage users to explore new sounds. If requested, provide brief descriptions of artists or genres, and explain why a recommendation might suit the user."
20
+ })
21
+
22
+ completion = client.chat.completions.create(
23
+ model="llama3-70b-8192",
24
+ messages=conversation_history,
25
+ temperature=1,
26
+ max_tokens=1024,
27
+ top_p=1,
28
+ stream=True,
29
+ stop=None,
30
+ )
31
+
32
+ response_content = ""
33
+ for chunk in completion:
34
+ response_content += chunk.choices[0].delta.content or ""
35
+
36
+ conversation_history.append({"role": "assistant", "content": response_content})
37
+
38
+ return [(msg["content"] if msg["role"] == "user" else None,
39
+ msg["content"] if msg["role"] == "assistant" else None)
40
+ for msg in conversation_history]
41
+
42
+ # Function to generate a storyboard
43
+ def generate_storyboard(scenario):
44
+ if not scenario.strip():
45
+ return "Please provide a scenario to generate the storyboard."
46
+
47
+ messages = [
48
+ {"role": "system", "content": """You are an AI storyteller. Generate a storyboard in a structured table with six scenes. For each scene you provide
49
+ 1) A Scenario text describing what problem a pesona is trying to resolve and by using what product or feature.
50
+ 2) Storyline text for each scene, descriptive visual information and the purpose of the scene.
51
+ You must provide the output in structured format like table.
52
+ """},
53
+ {"role": "user", "content": f"Generate a 6-scene storyboard for: {scenario}"}
54
+ ]
55
+
56
+ completion = client.chat.completions.create(
57
+ model="llama3-70b-8192",
58
+ messages=messages,
59
+ temperature=1,
60
+ max_tokens=1024,
61
+ top_p=1,
62
+ stream=False,
63
+ stop=None,
64
+ )
65
+ return completion.choices[0].message.content
66
+
67
+ TITLE = """
68
+ <style>
69
+ h1 { text-align: center; font-size: 24px; margin-bottom: 10px; }
70
+ </style>
71
+ <h1>📖 Storyboard Assistant</h1>
72
+ """
73
+
74
+ with gr.Blocks(theme=gr.themes.Glass(primary_hue="violet", secondary_hue="violet", neutral_hue="stone")) as demo:
75
+ with gr.Tabs():
76
+ with gr.TabItem("💬Chat"):
77
+ gr.HTML(TITLE)
78
+ chatbot = gr.Chatbot(label="Storyboard Chatbot")
79
+ with gr.Row():
80
+ user_input = gr.Textbox(
81
+ label="Your Message",
82
+ placeholder="Type your question here...",
83
+ lines=1
84
+ )
85
+ send_button = gr.Button("✋Ask Question")
86
+
87
+ # Chatbot functionality
88
+ send_button.click(
89
+ fn=chat_with_bot_stream,
90
+ inputs=user_input,
91
+ outputs=chatbot,
92
+ queue=True
93
+ ).then(
94
+ fn=lambda: "",
95
+ inputs=None,
96
+ outputs=user_input
97
+ )
98
+
99
+ with gr.TabItem("📖 Generate Storyboard"):
100
+ gr.Markdown("## Generate a Storyboard")
101
+ scenario_input = gr.Textbox(label="Enter your scenario")
102
+ generate_btn = gr.Button("Generate Storyboard")
103
+ storyboard_output = gr.Textbox(label="Generated Storyboard", interactive=False)
104
+ generate_btn.click(generate_storyboard, inputs=scenario_input, outputs=storyboard_output)
105
+
106
+ demo.launch()