MohamedSalah24 commited on
Commit
a0735ef
·
verified ·
1 Parent(s): d2af05d

Update.py

Browse files
Files changed (1) hide show
  1. app.py +65 -197
app.py CHANGED
@@ -1,204 +1,72 @@
1
- import os
2
- import time
3
- from typing import List, Tuple, Optional
4
- import google.generativeai as genai
5
  import gradio as gr
6
- from PIL import Image
7
- import tempfile
8
  import os
9
 
10
- GOOGLE_API_KEY = os.environ.get("GEMINI_API_KEY")
11
-
12
- IMAGE_WIDTH = 512
13
- IMAGE_WIDTH = 512
14
-
15
- system_instruction_analysis = "You are an expert of the given topic. Analyze the provided text with a focus on the topic, identifying recent issues, recent insights, or improvements relevant to academic standards and effectiveness. Offer actionable advice for enhancing knowledge and suggest real-life examples."
16
- model_name = "gemini-2.0-flash-exp"
17
- model = genai.GenerativeModel(model_name, system_instruction=system_instruction_analysis)
18
- #genai.configure(api_key=google_key)
19
-
20
- # Helper Functions
21
- def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
22
- return [seq.strip() for seq in stop_sequences.split(",")] if stop_sequences else None
23
-
24
- def preprocess_image(image: Image.Image) -> Image.Image:
25
- image_height = int(image.height * IMAGE_WIDTH / image.width)
26
- return image.resize((IMAGE_WIDTH, image_height))
27
-
28
- def user(text_prompt: str, chatbot: List[Tuple[str, str]]):
29
- return "", chatbot + [[text_prompt, None]]
30
-
31
- def bot(
32
- google_key: str,
33
- image_prompt: Optional[Image.Image],
34
- temperature: float,
35
- max_output_tokens: int,
36
- stop_sequences: str,
37
- top_k: int,
38
- top_p: float,
39
- chatbot: List[Tuple[str, str]]
40
- ):
41
- google_key = google_key or GOOGLE_API_KEY
42
- if not google_key:
43
- raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
44
-
45
- text_prompt = chatbot[-1][0].strip() if chatbot[-1][0] else None
46
-
47
- # Handle cases for text and/or image input
48
- if not text_prompt and not image_prompt:
49
- chatbot[-1][1] = "Prompt cannot be empty. Please provide input text or an image."
50
- yield chatbot
51
- return
52
- elif image_prompt and not text_prompt:
53
- # If only an image is provided
54
- text_prompt = "Describe the image"
55
- elif image_prompt and text_prompt:
56
- # If both text and image are provided, combine them
57
- text_prompt = f"{text_prompt}. Also, analyze the provided image."
58
-
59
- # Configure the model
60
- genai.configure(api_key=google_key)
61
- generation_config = genai.types.GenerationConfig(
62
- temperature=temperature,
63
- max_output_tokens=max_output_tokens,
64
- stop_sequences=preprocess_stop_sequences(stop_sequences),
65
- top_k=top_k,
66
- top_p=top_p,
67
- )
68
-
69
- # Prepare inputs
70
- inputs = [text_prompt] if image_prompt is None else [text_prompt, preprocess_image(image_prompt)]
71
-
72
- # Generate response
73
  try:
74
- response = model.generate_content(inputs, stream=True, generation_config=generation_config)
75
- response.resolve()
 
 
 
 
 
76
  except Exception as e:
77
- chatbot[-1][1] = f"Error occurred: {str(e)}"
78
- yield chatbot
79
- return
80
-
81
- # Stream the response back to the chatbot
82
- chatbot[-1][1] = ""
83
- for chunk in response:
84
- for i in range(0, len(chunk.text), 10):
85
- chatbot[-1][1] += chunk.text[i:i + 10]
86
- time.sleep(0.01)
87
- yield chatbot
88
- # Components
89
- google_key_component = gr.Textbox(
90
- label="Google API Key",
91
- type="password",
92
- placeholder="Enter your Google API Key",
93
- visible=GOOGLE_API_KEY is None
94
- )
95
-
96
- image_prompt_component = gr.Image(type="pil", label="Input Image (Optional: Figure/Graph)")
97
- chatbot_component = gr.Chatbot(label="Chatbot", bubble_full_width=False)
98
- text_prompt_component = gr.Textbox(
99
- placeholder="Type your question here...",
100
- label="Ask",
101
- lines=3
102
- )
103
- run_button_component = gr.Button("Submit")
104
- temperature_component = gr.Slider(
105
- minimum=0,
106
- maximum=1.0,
107
- value=0.4,
108
- step=0.05,
109
- label="Creativity (Temperature)",
110
- info="Controls the randomness of the response. Higher values result in more creative answers."
111
- )
112
- max_output_tokens_component = gr.Slider(
113
- minimum=1,
114
- maximum=2048,
115
- value=1024,
116
- step=1,
117
- label="Response Length (Token Limit)",
118
- info="Sets the maximum number of tokens in the output response."
119
- )
120
- stop_sequences_component = gr.Textbox(
121
- label="Stop Sequences (Optional)",
122
- placeholder="Enter stop sequences, e.g., STOP, END",
123
- info="Specify sequences to stop the generation."
124
  )
125
- top_k_component = gr.Slider(
126
- minimum=1,
127
- maximum=40,
128
- value=32,
129
- step=1,
130
- label="Top-K Sampling",
131
- info="Limits token selection to the top K most probable tokens. Lower values produce conservative outputs."
132
- )
133
- top_p_component = gr.Slider(
134
- minimum=0,
135
- maximum=1,
136
- value=1,
137
- step=0.01,
138
- label="Top-P Sampling",
139
- info="Limits token selection to tokens with a cumulative probability up to P. Lower values produce conservative outputs."
140
- )
141
- example_scenarios = [
142
- "Describe Multimodal AI",
143
- "What are the difference between muliagent llm and multiagent system",
144
- "Why it's difficult to intgrate multimodality in prompt"]
145
- example_images = [["ex1.png"],["ex2.png"]]
146
-
147
-
148
- # Gradio Interface
149
- user_inputs = [text_prompt_component, chatbot_component]
150
- bot_inputs = [
151
- google_key_component,
152
- image_prompt_component,
153
- temperature_component,
154
- max_output_tokens_component,
155
- stop_sequences_component,
156
- top_k_component,
157
- top_p_component,
158
- chatbot_component,
159
- ]
160
-
161
-
162
- with gr.Blocks(theme="earneleh/paris") as demo:
163
- gr.Markdown("<h1 style='font-size: 36px; font-weight: bold; font-family: Arial;'>Gemini 2.0 Multimodal Chatbot</h1>")
164
- with gr.Row():
165
- google_key_component.render()
166
- with gr.Row():
167
- chatbot_component.render()
168
- with gr.Row():
169
- with gr.Column(scale=0.5):
170
- text_prompt_component.render()
171
- with gr.Column(scale=0.5):
172
- image_prompt_component.render()
173
- with gr.Column(scale=0.5):
174
- run_button_component.render()
175
- with gr.Accordion("🧪Example Text 💬", open=False):
176
- example_radio = gr.Radio(
177
- choices=example_scenarios,
178
- label="Example Queries",
179
- info="Select an example query.")
180
- # Debug callback
181
- example_radio.change(
182
- fn=lambda query: query if query else "No query selected.",
183
- inputs=[example_radio],
184
- outputs=[text_prompt_component])
185
- # Custom examples section with blue styling
186
- with gr.Accordion("🧪Example Image 🩻", open=False):
187
- gr.Examples(
188
- examples=example_images,
189
- inputs=[image_prompt_component],
190
- label="Example Figures",
191
- )
192
- with gr.Accordion("🛠️Customize", open=False):
193
- temperature_component.render()
194
- max_output_tokens_component.render()
195
- stop_sequences_component.render()
196
- top_k_component.render()
197
- top_p_component.render()
198
 
199
- run_button_component.click(
200
- fn=user, inputs=user_inputs, outputs=[text_prompt_component, chatbot_component]
201
- ).then(
202
- fn=bot, inputs=bot_inputs, outputs=[chatbot_component]
203
- )
204
- demo.launch()
 
 
 
 
 
1
  import gradio as gr
2
+ from groq import Groq
 
3
  import os
4
 
5
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
6
+
7
+ SYSTEM_PROMPT = """You are an expert in storyboarding. Provide structured and insightful responses to queries
8
+ about creating and refining storyboards"""
9
+
10
+ def respond(message, history, model, temperature, max_tokens):
11
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
12
+
13
+ for h in history:
14
+ messages.append({"role": "user", "content": h[0]})
15
+ if h[1]:
16
+ messages.append({"role": "assistant", "content": h[1]})
17
+
18
+ messages.append({"role": "user", "content": message})
19
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  try:
21
+ response = client.chat.completions.create(
22
+ model=model,
23
+ messages=messages,
24
+ temperature=temperature,
25
+ max_completion_tokens=max_tokens,
26
+ )
27
+ return response.choices[0].message.content
28
  except Exception as e:
29
+ return f"Error: {str(e)}"
30
+
31
+ # ChatInterface with additional inputs for parameters
32
+ demo = gr.ChatInterface(
33
+ fn=respond,
34
+ title="🎬 Storyboard Generator AI",
35
+ description="Create professional storyboards for films, animations, and more!",
36
+ additional_inputs=[
37
+ gr.Dropdown(
38
+ choices=[
39
+ "llama-3.3-70b-versatile",
40
+ "llama-3.1-8b-instant",
41
+ ],
42
+ value="llama-3.3-70b-versatile",
43
+ label="Model",
44
+ info="Select the AI model to use"
45
+ ),
46
+ gr.Slider(
47
+ minimum=0,
48
+ maximum=2,
49
+ value=0.9,
50
+ step=0.1,
51
+ label="Temperature",
52
+ info="Controls randomness. Lower = more focused, Higher = more creative"
53
+ ),
54
+ gr.Slider(
55
+ minimum=256,
56
+ maximum=8192,
57
+ value=2048,
58
+ step=256,
59
+ label="Max Tokens",
60
+ info="Maximum length of the response"
61
+ ),
62
+ ],
63
+ examples=[
64
+ ["Create a storyboard for a 30-second coffee commercial"],
65
+ ["Generate a horror movie opening scene storyboard"],
66
+ ["Design a storyboard for a romantic comedy meet-cute at a bookstore"],
67
+ ],
68
+ theme="soft",
 
 
 
 
 
 
 
69
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
+ if __name__ == "__main__":
72
+ demo.launch()