suayptalha commited on
Commit
c617e3e
·
verified ·
1 Parent(s): 890da26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -56
app.py CHANGED
@@ -1,72 +1,35 @@
1
  import gradio as gr
2
  from gradio_client import Client, handle_file
3
 
4
- # Gradio Client ile model yükle
5
- client = Client("vikhyatk/moondream2")
6
 
7
- # Resim açıklamasını almak için kullanılacak fonksiyon
8
- def describe_image(image):
 
 
 
 
 
9
  result = client.predict(
10
- img=handle_file(image),
11
  prompt="Describe this image.",
12
  api_name="/answer_question"
13
  )
14
  return result
15
 
16
- # ChatInterface ve resim işleme fonksiyonu
17
- def respond(
18
- message,
19
- history=None, # Varsayılan olarak history None
20
- system_message="You are a friendly Chatbot.",
21
- max_tokens=512,
22
- temperature=0.7,
23
- top_p=0.95,
24
- image=None
25
- ):
26
- if history is None:
27
- history = [] # History boşsa, boş bir liste başlat
28
- elif isinstance(history, int): # Eğer history bir int ise, bunu boş bir liste ile değiştir
29
- history = []
30
 
31
- # Sistem mesajını ve önceki sohbeti al
32
- messages = [{"role": "system", "content": system_message}]
 
33
 
34
- for val in history:
35
- if val[0]:
36
- messages.append({"role": "user", "content": val[0]})
37
- if val[1]:
38
- messages.append({"role": "assistant", "content": val[1]})
39
-
40
- # Resim ile ilgili açıklamayı ekle
41
- if image is not None:
42
- image_description = describe_image(image)
43
- messages.append({"role": "assistant", "content": image_description})
44
-
45
- # Kullanıcı mesajını ekle
46
- messages.append({"role": "user", "content": message})
47
-
48
- # Modelden gelen yanıtı al
49
- response = client.chat(
50
- messages=messages,
51
- max_tokens=max_tokens,
52
- temperature=temperature,
53
- top_p=top_p
54
- )
55
 
56
- return response['choices'][0]['message']['content']
57
 
58
- # Gradio app arayüzünü oluştur
59
- demo = gr.Interface(
60
- fn=respond,
61
- inputs=[
62
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
63
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
64
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
65
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
66
- gr.Image(type="pil", label="Upload an Image") # Resim girişi
67
- ],
68
- outputs="text", # Çıktıyı metin olarak ver
69
- )
70
 
71
  if __name__ == "__main__":
72
- demo.launch()
 
1
  import gradio as gr
2
  from gradio_client import Client, handle_file
3
 
4
+ # Gradio Client
5
+ client = Client("vikhyatk/moondream2") # Replace with your Gradio app URL or identifier
6
 
7
+
8
+ def process_image_upload(image):
9
+ """
10
+ This function processes the uploaded image and sends it to the Gradio app.
11
+ """
12
+ # Convert the uploaded image to a format compatible with the Gradio Client
13
+ image_file = handle_file(image) # Handle the uploaded image as a file
14
  result = client.predict(
15
+ img=image_file,
16
  prompt="Describe this image.",
17
  api_name="/answer_question"
18
  )
19
  return result
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ # Gradio UI
23
+ with gr.Blocks() as demo:
24
+ gr.Markdown("## Image Description with Gradio Client")
25
 
26
+ with gr.Row():
27
+ image_input = gr.Image(type="file", label="Upload an Image")
28
+ output_text = gr.Textbox(label="Image Description", interactive=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
+ submit_button = gr.Button("Get Description")
31
 
32
+ submit_button.click(fn=process_image_upload, inputs=image_input, outputs=output_text)
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  if __name__ == "__main__":
35
+ demo.launch()