Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,33 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
-
import wikipedia
|
4 |
-
from PIL import Image
|
5 |
-
import requests
|
6 |
-
import torch
|
7 |
-
from torchvision import transforms
|
8 |
-
from torchvision.models import resnet50
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
|
15 |
-
# Load pre-trained image classification model
|
16 |
-
model = resnet50(pretrained=True)
|
17 |
-
model.eval()
|
18 |
-
transform = transforms.Compose([
|
19 |
-
transforms.Resize(256),
|
20 |
-
transforms.CenterCrop(224),
|
21 |
-
transforms.ToTensor(),
|
22 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
23 |
-
])
|
24 |
|
25 |
-
def
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
# Search Wikipedia for information
|
34 |
-
search_response = search_wikipedia(message)
|
35 |
-
|
36 |
-
# Prepare the chat messages
|
37 |
messages = [{"role": "system", "content": system_message}]
|
|
|
38 |
for val in history:
|
39 |
if val[0]:
|
40 |
messages.append({"role": "user", "content": val[0]})
|
41 |
if val[1]:
|
42 |
messages.append({"role": "assistant", "content": val[1]})
|
43 |
-
|
44 |
messages.append({"role": "user", "content": message})
|
45 |
-
|
46 |
-
# Generate response from chat model
|
47 |
response = ""
|
48 |
-
|
|
|
49 |
messages,
|
50 |
max_tokens=max_tokens,
|
51 |
stream=True,
|
@@ -53,67 +35,30 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
53 |
top_p=top_p,
|
54 |
):
|
55 |
token = message.choices[0].delta.content
|
56 |
-
response += token
|
57 |
-
yield response, search_response # Return both responses
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
with torch.no_grad():
|
62 |
-
output = model(image)
|
63 |
-
_, predicted = torch.max(output, 1)
|
64 |
-
return f"Predicted class index: {predicted.item()}"
|
65 |
-
|
66 |
-
# Gradio interface setup using Blocks
|
67 |
-
with gr.Blocks() as demo:
|
68 |
-
gr.Markdown("## Multi-Functional AI Interface")
|
69 |
-
|
70 |
-
with gr.Tab("Chatbot with Wikipedia Search"):
|
71 |
-
with gr.Row():
|
72 |
-
with gr.Column():
|
73 |
-
system_message = gr.Textbox(value="You are a friendly Chatbot named Tirth.", label="System message")
|
74 |
-
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
75 |
-
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
76 |
-
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
77 |
-
|
78 |
-
with gr.Column():
|
79 |
-
chat_output = gr.Chatbot(label="Chat History")
|
80 |
-
user_input = gr.Textbox(placeholder="Type your message here...", label="Your Message")
|
81 |
-
submit_btn = gr.Button("Send")
|
82 |
-
|
83 |
-
def on_submit(message, history):
|
84 |
-
response, search_response = respond(message, history, system_message.value, max_tokens.value, temperature.value, top_p.value)
|
85 |
-
return history + [(message, response)], search_response
|
86 |
-
|
87 |
-
submit_btn.click(on_submit, inputs=[user_input, chat_output], outputs=[chat_output, gr.Textbox(label="Wikipedia Summary")])
|
88 |
-
|
89 |
-
with gr.Tab("Image Classification"):
|
90 |
-
image_input = gr.Image(type="pil", label="Upload an Image")
|
91 |
-
classify_btn = gr.Button("Classify Image")
|
92 |
-
classification_output = gr.Textbox(label="Classification Result")
|
93 |
-
|
94 |
-
classify_btn.click(classify_image, inputs=image_input, outputs=classification_output)
|
95 |
-
|
96 |
-
with gr.Tab("Video Generation"):
|
97 |
-
video_input = gr.Video(label="Upload a Video")
|
98 |
-
generate_video_btn = gr.Button("Generate Video")
|
99 |
-
video_output = gr.Video(label="Generated Video")
|
100 |
-
|
101 |
-
# Placeholder for video generation logic (implement as needed)
|
102 |
-
def generate_video(video):
|
103 |
-
return video # Just returns the input video for now
|
104 |
|
105 |
-
generate_video_btn.click(generate_video, inputs=video_input, outputs=video_output)
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
-
classify_video_btn.click(classify_video, inputs=video_class_input, outputs=video_classification_output)
|
117 |
|
118 |
if __name__ == "__main__":
|
119 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
"""
|
5 |
+
For more information on huggingface_hub Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
+
"""
|
7 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
def respond(
|
11 |
+
message,
|
12 |
+
history: list[tuple[str, str]],
|
13 |
+
system_message,
|
14 |
+
max_tokens,
|
15 |
+
temperature,
|
16 |
+
top_p,
|
17 |
+
):
|
|
|
|
|
|
|
|
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
+
|
20 |
for val in history:
|
21 |
if val[0]:
|
22 |
messages.append({"role": "user", "content": val[0]})
|
23 |
if val[1]:
|
24 |
messages.append({"role": "assistant", "content": val[1]})
|
25 |
+
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
+
|
|
|
28 |
response = ""
|
29 |
+
|
30 |
+
for message in client.chat_completion(
|
31 |
messages,
|
32 |
max_tokens=max_tokens,
|
33 |
stream=True,
|
|
|
35 |
top_p=top_p,
|
36 |
):
|
37 |
token = message.choices[0].delta.content
|
|
|
|
|
38 |
|
39 |
+
response += token
|
40 |
+
yield response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
|
|
42 |
|
43 |
+
"""
|
44 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
+
"""
|
46 |
+
demo = gr.ChatInterface(
|
47 |
+
respond,
|
48 |
+
additional_inputs=[
|
49 |
+
gr.Textbox(value="You are a friendly Chatbot named Tirth.", label="System message"),
|
50 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
+
gr.Slider(
|
53 |
+
minimum=0.1,
|
54 |
+
maximum=1.0,
|
55 |
+
value=0.95,
|
56 |
+
step=0.05,
|
57 |
+
label="Top-p (nucleus sampling)",
|
58 |
+
),
|
59 |
+
],
|
60 |
+
)
|
61 |
|
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
+
demo.launch()
|