Update app.py
Browse files
app.py
CHANGED
|
@@ -1,104 +1,110 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
-
CHATBOT_CLIENT = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
-
|
| 7 |
-
# Image Generation Model Configuration
|
| 8 |
-
IMAGE_CLIENT = InferenceClient("black-forest-labs/FLUX.1-schnell")
|
| 9 |
-
|
| 10 |
-
def chatbot_respond(
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message="You are a friendly Chatbot.",
|
| 14 |
-
max_tokens=512,
|
| 15 |
-
temperature=0.7,
|
| 16 |
-
top_p=0.95,
|
| 17 |
-
):
|
| 18 |
"""
|
| 19 |
-
|
| 20 |
"""
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
for message in CHATBOT_CLIENT.chat_completion(
|
| 33 |
-
messages,
|
| 34 |
-
max_tokens=max_tokens,
|
| 35 |
-
stream=True,
|
| 36 |
-
temperature=temperature,
|
| 37 |
-
top_p=top_p,
|
| 38 |
-
):
|
| 39 |
-
token = message.choices[0].delta.content
|
| 40 |
-
response += token
|
| 41 |
-
yield response
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
return f"Error generating image: {str(e)}"
|
| 55 |
|
| 56 |
-
def
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
step=0.05,
|
| 75 |
-
label="Top-p (nucleus sampling)",
|
| 76 |
-
),
|
| 77 |
-
]
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
with gr.Tab("Image Generation"):
|
| 81 |
-
with gr.Row():
|
| 82 |
-
image_prompt = gr.Textbox(label="Enter Image Prompt")
|
| 83 |
-
generate_btn = gr.Button("Generate Image")
|
| 84 |
-
|
| 85 |
-
image_output = gr.Image(label="Generated Image")
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
|
| 101 |
# Launch the combined interface
|
| 102 |
if __name__ == "__main__":
|
| 103 |
-
|
|
|
|
| 104 |
combined_demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
class AICompanion:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
"""
|
| 6 |
+
Combined AI Companion with Chatbot and Image Generation
|
| 7 |
"""
|
| 8 |
+
def __init__(self):
|
| 9 |
+
# Chatbot Model Configuration
|
| 10 |
+
self.chatbot_client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 11 |
+
|
| 12 |
+
# Image Generation Model Configuration
|
| 13 |
+
self.image_client = InferenceClient("black-forest-labs/FLUX.1-schnell")
|
| 14 |
|
| 15 |
+
def chatbot_respond(
|
| 16 |
+
self,
|
| 17 |
+
message,
|
| 18 |
+
history: list[tuple[str, str]],
|
| 19 |
+
system_message="You are a friendly Chatbot.",
|
| 20 |
+
max_tokens=512,
|
| 21 |
+
temperature=0.7,
|
| 22 |
+
top_p=0.95,
|
| 23 |
+
):
|
| 24 |
+
"""
|
| 25 |
+
Generate chatbot responses using Hugging Face's Zephyr model
|
| 26 |
+
"""
|
| 27 |
+
messages = [{"role": "system", "content": system_message}]
|
| 28 |
|
| 29 |
+
for val in history:
|
| 30 |
+
if val[0]:
|
| 31 |
+
messages.append({"role": "user", "content": val[0]})
|
| 32 |
+
if val[1]:
|
| 33 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 34 |
|
| 35 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
response = ""
|
| 38 |
+
for message in self.chatbot_client.chat_completion(
|
| 39 |
+
messages,
|
| 40 |
+
max_tokens=max_tokens,
|
| 41 |
+
stream=True,
|
| 42 |
+
temperature=temperature,
|
| 43 |
+
top_p=top_p,
|
| 44 |
+
):
|
| 45 |
+
token = message.choices[0].delta.content or ""
|
| 46 |
+
response += token
|
| 47 |
+
yield response
|
|
|
|
| 48 |
|
| 49 |
+
def generate_image(self, prompt):
|
| 50 |
+
"""
|
| 51 |
+
Generate image using Flux.1-schnell model
|
| 52 |
+
"""
|
| 53 |
+
if not prompt:
|
| 54 |
+
return None
|
| 55 |
|
| 56 |
+
try:
|
| 57 |
+
return self.image_client.text_to_image(prompt)
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return f"Error generating image: {str(e)}"
|
| 60 |
+
|
| 61 |
+
def create_interface(self):
|
| 62 |
+
"""
|
| 63 |
+
Create a combined Gradio interface with chatbot and image generation
|
| 64 |
+
"""
|
| 65 |
+
with gr.Blocks() as demo:
|
| 66 |
+
gr.Markdown("# AI Companion: Chat and Image Generation")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
with gr.Tab("Chatbot"):
|
| 69 |
+
chatbot_interface = gr.ChatInterface(
|
| 70 |
+
self.chatbot_respond,
|
| 71 |
+
additional_inputs=[
|
| 72 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 73 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 74 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 75 |
+
gr.Slider(
|
| 76 |
+
minimum=0.1,
|
| 77 |
+
maximum=1.0,
|
| 78 |
+
value=0.95,
|
| 79 |
+
step=0.05,
|
| 80 |
+
label="Top-p (nucleus sampling)",
|
| 81 |
+
),
|
| 82 |
+
]
|
| 83 |
+
)
|
| 84 |
|
| 85 |
+
with gr.Tab("Image Generation"):
|
| 86 |
+
with gr.Row():
|
| 87 |
+
image_prompt = gr.Textbox(label="Enter Image Prompt")
|
| 88 |
+
generate_btn = gr.Button("Generate Image")
|
| 89 |
+
|
| 90 |
+
image_output = gr.Image(label="Generated Image")
|
| 91 |
+
|
| 92 |
+
generate_btn.click(
|
| 93 |
+
fn=self.generate_image,
|
| 94 |
+
inputs=image_prompt,
|
| 95 |
+
outputs=image_output
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
image_prompt.submit(
|
| 99 |
+
fn=self.generate_image,
|
| 100 |
+
inputs=image_prompt,
|
| 101 |
+
outputs=image_output
|
| 102 |
+
)
|
| 103 |
|
| 104 |
+
return demo
|
| 105 |
|
| 106 |
# Launch the combined interface
|
| 107 |
if __name__ == "__main__":
|
| 108 |
+
companion = AICompanion()
|
| 109 |
+
combined_demo = companion.create_interface()
|
| 110 |
combined_demo.launch()
|