Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,69 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
3 |
import torch
|
|
|
|
|
|
|
4 |
|
5 |
-
# Define a dictionary of
|
6 |
-
|
7 |
-
"
|
8 |
-
"
|
9 |
-
"
|
10 |
-
"
|
|
|
|
|
11 |
}
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def chat(model_name, user_input, history=[]):
|
22 |
-
tokenizer =
|
23 |
-
model =
|
24 |
|
25 |
# Encode the input
|
26 |
input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
|
@@ -39,17 +82,167 @@ def chat(model_name, user_input, history=[]):
|
|
39 |
|
40 |
return history, history
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
# Define the Gradio interface
|
43 |
with gr.Blocks() as demo:
|
44 |
-
gr.Markdown("##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
53 |
|
54 |
# Launch the demo
|
55 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
3 |
+
from diffusers import StableDiffusionPipeline
|
4 |
import torch
|
5 |
+
import requests
|
6 |
+
from PIL import Image
|
7 |
+
import io
|
8 |
|
9 |
+
# Define a dictionary of conversational models
|
10 |
+
conversational_models = {
|
11 |
+
"Qwen": "Qwen/QwQ-32B",
|
12 |
+
"DeepSeek R1": "deepseek-ai/DeepSeek-R1",
|
13 |
+
"Perplexity (R1 Post-trained)": "perplexity-ai/r1-1776",
|
14 |
+
"Llama-Instruct by Meta": "meta-llama/Llama-3.2-3B-Instruct",
|
15 |
+
"Mistral": "mistralai/Mistral-7B-v0.1",
|
16 |
+
"Gemma": "google/gemma-2-2b-it",
|
17 |
}
|
18 |
|
19 |
+
# Define a dictionary of Text-to-Image models
|
20 |
+
text_to_image_models = {
|
21 |
+
"Stable Diffusion 3.5 Large": "stabilityai/stable-diffusion-3.5-large",
|
22 |
+
"Stable Diffusion 1.4": "CompVis/stable-diffusion-v1-4",
|
23 |
+
"Flux Dev": "black-forest-labs/FLUX.1-dev",
|
24 |
+
}
|
25 |
+
|
26 |
+
# Define a dictionary of Text-to-Speech models
|
27 |
+
text_to_speech_models = {
|
28 |
+
"Spark TTS": "SparkAudio/Spark-TTS-0.5B",
|
29 |
+
}
|
30 |
+
|
31 |
+
# Initialize tokenizers and models for conversational AI
|
32 |
+
conversational_tokenizers = {}
|
33 |
+
conversational_models_loaded = {}
|
34 |
+
|
35 |
+
for model_name, model_id in conversational_models.items():
|
36 |
+
conversational_tokenizers[model_name] = AutoTokenizer.from_pretrained(model_id)
|
37 |
+
conversational_models_loaded[model_name] = AutoModelForCausalLM.from_pretrained(model_id)
|
38 |
+
|
39 |
+
# Initialize pipelines for Text-to-Image
|
40 |
+
text_to_image_pipelines = {}
|
41 |
+
|
42 |
+
for model_name, model_id in text_to_image_models.items():
|
43 |
+
text_to_image_pipelines[model_name] = StableDiffusionPipeline.from_pretrained(model_id)
|
44 |
+
|
45 |
+
# Initialize pipelines for Text-to-Speech
|
46 |
+
text_to_speech_pipelines = {}
|
47 |
+
|
48 |
+
for model_name, model_id in text_to_speech_models.items():
|
49 |
+
text_to_speech_pipelines[model_name] = pipeline("text-to-speech", model=model_id)
|
50 |
|
51 |
+
# Initialize pipelines for other tasks
|
52 |
+
visual_qa_pipeline = pipeline("visual-question-answering", model="dandelin/vilt-b32-finetuned-vqa")
|
53 |
+
document_qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
54 |
+
image_classification_pipeline = pipeline("image-classification", model="facebook/detr-resnet-50")
|
55 |
+
object_detection_pipeline = pipeline("object-detection", model="facebook/detr-resnet-50")
|
56 |
+
video_classification_pipeline = pipeline("video-classification", model="facebook/x3d-l")
|
57 |
+
text_to_3d_pipeline = pipeline("text-to-3d", model="CompVis/td2s")
|
58 |
+
keypoint_detection_pipeline = pipeline("keypoint-detection", model="facebook/detr-resnet-50")
|
59 |
+
translation_pipeline = pipeline("translation_en_to_fr", model="Helsinki-NLP/opus-mt-en-fr")
|
60 |
+
summarization_pipeline = pipeline("summarization", model="facebook/bart-large-cnn")
|
61 |
+
text_to_audio_pipeline = pipeline("text-to-speech", model="julien-c/ljspeech_tts_train_tacotron2_raw_phn_tacotron_g2p_en_no_space")
|
62 |
+
audio_classification_pipeline = pipeline("audio-classification", model="facebook/wav2vec2-base")
|
63 |
|
64 |
def chat(model_name, user_input, history=[]):
|
65 |
+
tokenizer = conversational_tokenizers[model_name]
|
66 |
+
model = conversational_models_loaded[model_name]
|
67 |
|
68 |
# Encode the input
|
69 |
input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
|
|
|
82 |
|
83 |
return history, history
|
84 |
|
85 |
+
def generate_image(model_name, prompt):
|
86 |
+
pipeline = text_to_image_pipelines[model_name]
|
87 |
+
image = pipeline(prompt).images[0]
|
88 |
+
return image
|
89 |
+
|
90 |
+
def generate_speech(model_name, text):
|
91 |
+
pipeline = text_to_speech_pipelines[model_name]
|
92 |
+
audio = pipeline(text)
|
93 |
+
return audio["audio"]
|
94 |
+
|
95 |
+
def visual_qa(image, question):
|
96 |
+
result = visual_qa_pipeline(image, question)
|
97 |
+
return result["answer"]
|
98 |
+
|
99 |
+
def document_qa(document, question):
|
100 |
+
result = document_qa_pipeline(question=question, context=document)
|
101 |
+
return result["answer"]
|
102 |
+
|
103 |
+
def image_classification(image):
|
104 |
+
result = image_classification_pipeline(image)
|
105 |
+
return result
|
106 |
+
|
107 |
+
def object_detection(image):
|
108 |
+
result = object_detection_pipeline(image)
|
109 |
+
return result
|
110 |
+
|
111 |
+
def video_classification(video):
|
112 |
+
result = video_classification_pipeline(video)
|
113 |
+
return result
|
114 |
+
|
115 |
+
def text_to_3d(text):
|
116 |
+
result = text_to_3d_pipeline(text)
|
117 |
+
return result["image"]
|
118 |
+
|
119 |
+
def keypoint_detection(image):
|
120 |
+
result = keypoint_detection_pipeline(image)
|
121 |
+
return result
|
122 |
+
|
123 |
+
def translate_text(text):
|
124 |
+
result = translation_pipeline(text)
|
125 |
+
return result[0]["translation_text"]
|
126 |
+
|
127 |
+
def summarize_text(text):
|
128 |
+
result = summarization_pipeline(text)
|
129 |
+
return result[0]["summary_text"]
|
130 |
+
|
131 |
+
def text_to_audio(text):
|
132 |
+
result = text_to_audio_pipeline(text)
|
133 |
+
return result["audio"]
|
134 |
+
|
135 |
+
def audio_classification(audio):
|
136 |
+
result = audio_classification_pipeline(audio)
|
137 |
+
return result
|
138 |
+
|
139 |
# Define the Gradio interface
|
140 |
with gr.Blocks() as demo:
|
141 |
+
gr.Markdown("## Versatile AI Chatbot and Text-to-X Tasks")
|
142 |
+
|
143 |
+
with gr.Tab("Conversational AI"):
|
144 |
+
conversational_model_choice = gr.Dropdown(list(conversational_models.keys()), label="Choose a Conversational Model")
|
145 |
+
conversational_chatbot = gr.Chatbot(label="Chat")
|
146 |
+
conversational_message = gr.Textbox(label="Message")
|
147 |
+
conversational_submit = gr.Button("Submit")
|
148 |
+
|
149 |
+
conversational_submit.click(chat, inputs=[conversational_model_choice, conversational_message, conversational_chatbot], outputs=[conversational_chatbot, conversational_chatbot])
|
150 |
+
conversational_message.submit(chat, inputs=[conversational_model_choice, conversational_message, conversational_chatbot], outputs=[conversational_chatbot, conversational_chatbot])
|
151 |
+
|
152 |
+
with gr.Tab("Text-to-Image"):
|
153 |
+
text_to_image_model_choice = gr.Dropdown(list(text_to_image_models.keys()), label="Choose a Text-to-Image Model")
|
154 |
+
text_to_image_prompt = gr.Textbox(label="Prompt")
|
155 |
+
text_to_image_generate = gr.Button("Generate Image")
|
156 |
+
text_to_image_output = gr.Image(label="Generated Image")
|
157 |
+
|
158 |
+
text_to_image_generate.click(generate_image, inputs=[text_to_image_model_choice, text_to_image_prompt], outputs=text_to_image_output)
|
159 |
+
|
160 |
+
with gr.Tab("Text-to-Speech"):
|
161 |
+
text_to_speech_model_choice = gr.Dropdown(list(text_to_speech_models.keys()), label="Choose a Text-to-Speech Model")
|
162 |
+
text_to_speech_text = gr.Textbox(label="Text")
|
163 |
+
text_to_speech_generate = gr.Button("Generate Speech")
|
164 |
+
text_to_speech_output = gr.Audio(label="Generated Speech")
|
165 |
+
|
166 |
+
text_to_speech_generate.click(generate_speech, inputs=[text_to_speech_model_choice, text_to_speech_text], outputs=text_to_speech_output)
|
167 |
+
|
168 |
+
with gr.Tab("Visual Question Answering"):
|
169 |
+
visual_qa_image = gr.Image(label="Upload Image")
|
170 |
+
visual_qa_question = gr.Textbox(label="Question")
|
171 |
+
visual_qa_generate = gr.Button("Answer")
|
172 |
+
visual_qa_output = gr.Textbox(label="Answer")
|
173 |
+
|
174 |
+
visual_qa_generate.click(visual_qa, inputs=[visual_qa_image, visual_qa_question], outputs=visual_qa_output)
|
175 |
+
|
176 |
+
with gr.Tab("Document Question Answering"):
|
177 |
+
document_qa_document = gr.Textbox(label="Document Text")
|
178 |
+
document_qa_question = gr.Textbox(label="Question")
|
179 |
+
document_qa_generate = gr.Button("Answer")
|
180 |
+
document_qa_output = gr.Textbox(label="Answer")
|
181 |
+
|
182 |
+
document_qa_generate.click(document_qa, inputs=[document_qa_document, document_qa_question], outputs=document_qa_output)
|
183 |
+
|
184 |
+
with gr.Tab("Image Classification"):
|
185 |
+
image_classification_image = gr.Image(label="Upload Image")
|
186 |
+
image_classification_generate = gr.Button("Classify")
|
187 |
+
image_classification_output = gr.Textbox(label="Classification Result")
|
188 |
+
|
189 |
+
image_classification_generate.click(image_classification, inputs=image_classification_image, outputs=image_classification_output)
|
190 |
+
|
191 |
+
with gr.Tab("Object Detection"):
|
192 |
+
object_detection_image = gr.Image(label="Upload Image")
|
193 |
+
object_detection_generate = gr.Button("Detect")
|
194 |
+
object_detection_output = gr.Image(label="Detection Result")
|
195 |
+
|
196 |
+
object_detection_generate.click(object_detection, inputs=object_detection_image, outputs=object_detection_output)
|
197 |
+
|
198 |
+
with gr.Tab("Video Classification"):
|
199 |
+
video_classification_video = gr.Video(label="Upload Video")
|
200 |
+
video_classification_generate = gr.Button("Classify")
|
201 |
+
video_classification_output = gr.Textbox(label="Classification Result")
|
202 |
+
|
203 |
+
video_classification_generate.click(video_classification, inputs=video_classification_video, outputs=video_classification_output)
|
204 |
+
|
205 |
+
with gr.Tab("Text-to-3D"):
|
206 |
+
text_to_3d_text = gr.Textbox(label="Text")
|
207 |
+
text_to_3d_generate = gr.Button("Generate 3D")
|
208 |
+
text_to_3d_output = gr.Image(label="3D Model")
|
209 |
+
|
210 |
+
text_to_3d_generate.click(text_to_3d, inputs=text_to_3d_text, outputs=text_to_3d_output)
|
211 |
+
|
212 |
+
with gr.Tab("Keypoint Detection"):
|
213 |
+
keypoint_detection_image = gr.Image(label="Upload Image")
|
214 |
+
keypoint_detection_generate = gr.Button("Detect Keypoints")
|
215 |
+
keypoint_detection_output = gr.Image(label="Keypoint Detection Result")
|
216 |
+
|
217 |
+
keypoint_detection_generate.click(keypoint_detection, inputs=keypoint_detection_image, outputs=keypoint_detection_output)
|
218 |
+
|
219 |
+
with gr.Tab("Translation"):
|
220 |
+
translate_text_text = gr.Textbox(label="Text")
|
221 |
+
translate_text_generate = gr.Button("Translate")
|
222 |
+
translate_text_output = gr.Textbox(label="Translated Text")
|
223 |
+
|
224 |
+
translate_text_generate.click(translate_text, inputs=translate_text_text, outputs=translate_text_output)
|
225 |
+
|
226 |
+
with gr.Tab("Summarization"):
|
227 |
+
summarize_text_text = gr.Textbox(label="Text")
|
228 |
+
summarize_text_generate = gr.Button("Summarize")
|
229 |
+
summarize_text_output = gr.Textbox(label="Summary")
|
230 |
+
|
231 |
+
summarize_text_generate.click(summarize_text, inputs=summarize_text_text, outputs=summarize_text_output)
|
232 |
|
233 |
+
with gr.Tab("Text-to-Audio"):
|
234 |
+
text_to_audio_text = gr.Textbox(label="Text")
|
235 |
+
text_to_audio_generate = gr.Button("Generate Audio")
|
236 |
+
text_to_audio_output = gr.Audio(label="Generated Audio")
|
237 |
+
|
238 |
+
text_to_audio_generate.click(text_to_audio, inputs=text_to_audio_text, outputs=text_to_audio_output)
|
239 |
|
240 |
+
with gr.Tab("Audio Classification"):
|
241 |
+
audio_classification_audio = gr.Audio(label="Upload Audio")
|
242 |
+
audio_classification_generate = gr.Button("Classify")
|
243 |
+
audio_classification_output = gr.Textbox(label="Classification Result")
|
244 |
+
|
245 |
+
audio_classification_generate.click(audio_classification, inputs=audio_classification_audio, outputs=audio_classification_output)
|
246 |
|
247 |
# Launch the demo
|
248 |
demo.launch()
|