TirthGPT commited on
Commit
cfc28f3
·
verified ·
1 Parent(s): 802ec79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -93
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
- # Initialize inference client for chat
11
- chat_client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
12
- # Initialize Wikipedia API
13
- wiki_wiki = wikipediaapi.Wikipedia('en')
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 search_wikipedia(query):
26
- page = wiki_wiki.page(query)
27
- if page.exists():
28
- return page.summary
29
- else:
30
- return "No information found on that topic."
31
-
32
- def respond(message, history, system_message, max_tokens, temperature, top_p):
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
- for message in chat_client.chat_completion(
 
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
- def classify_image(image):
60
- image = transform(image).unsqueeze(0)
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
- with gr.Tab("Video Classification"):
108
- video_class_input = gr.Video(label="Upload a Video for Classification")
109
- classify_video_btn = gr.Button("Classify Video")
110
- video_classification_output = gr.Textbox(label="Video Classification Result")
111
-
112
- # Placeholder for video classification logic (implement as needed)
113
- def classify_video(video):
114
- return "Video classification logic not implemented." # Placeholder
 
 
 
 
 
 
 
 
 
 
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()