xzerus commited on
Commit
7d059af
·
verified ·
1 Parent(s): c9c847a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -9
app.py CHANGED
@@ -67,7 +67,7 @@ model = AutoModel.from_pretrained(
67
 
68
  tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
69
 
70
- # Define the function for Gradio interface
71
  def process_image(image):
72
  try:
73
  pixel_values = load_image(image, max_num=12).to(torch.bfloat16).cuda()
@@ -78,14 +78,31 @@ def process_image(image):
78
  except Exception as e:
79
  return f"Error: {str(e)}"
80
 
81
- # Create Gradio Interface
82
- demo = gr.Interface(
83
- fn=process_image,
84
- inputs=gr.Image(type="pil"),
85
- outputs="text",
86
- title="Dynamic Image Processing with InternVL",
87
- description="Upload an image and get detailed responses using the InternVL model."
88
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  # Launch the Gradio app
91
  if __name__ == "__main__":
 
67
 
68
  tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
69
 
70
+ # Define the function for Gradio image interface
71
  def process_image(image):
72
  try:
73
  pixel_values = load_image(image, max_num=12).to(torch.bfloat16).cuda()
 
78
  except Exception as e:
79
  return f"Error: {str(e)}"
80
 
81
+ # Define the function for text-based chatbot interface
82
+ def chatbot(input_text, history=[]):
83
+ try:
84
+ generation_config = dict(max_new_tokens=1024, do_sample=True)
85
+ response, updated_history = model.chat(tokenizer, None, input_text, generation_config, history=history, return_history=True)
86
+ return response, updated_history
87
+ except Exception as e:
88
+ return f"Error: {str(e)}", history
89
+
90
+ # Create Gradio Tabs
91
+ with gr.Blocks() as demo:
92
+ with gr.Tab("Image Processing"):
93
+ gr.Markdown("Upload an image and get detailed responses using the InternVL model.")
94
+ image_input = gr.Image(type="pil")
95
+ image_output = gr.Textbox(label="Response")
96
+ image_btn = gr.Button("Process")
97
+ image_btn.click(process_image, inputs=image_input, outputs=image_output)
98
+
99
+ with gr.Tab("Chatbot"):
100
+ gr.Markdown("Chat with the model.")
101
+ chatbot_input = gr.Textbox(label="Your Message")
102
+ chatbot_output = gr.Textbox(label="Response")
103
+ chatbot_history = gr.State([])
104
+ chatbot_btn = gr.Button("Send")
105
+ chatbot_btn.click(chatbot, inputs=[chatbot_input, chatbot_history], outputs=[chatbot_output, chatbot_history])
106
 
107
  # Launch the Gradio app
108
  if __name__ == "__main__":