suayptalha commited on
Commit
d8ff437
·
verified ·
1 Parent(s): d44322e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -22
app.py CHANGED
@@ -1,12 +1,20 @@
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]],
@@ -14,19 +22,26 @@ def respond(
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,
@@ -35,30 +50,21 @@ def respond(
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.", 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()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ from gradio_client import Client, handle_file
4
 
5
+ # Gradio Client
6
+ client = Client("vikhyatk/moondream2")
 
 
7
 
8
+ # Resmi tanımlayan fonksiyon
9
+ def describe_image(image):
10
+ result = client.predict(
11
+ img=handle_file(image),
12
+ prompt="Describe this image.",
13
+ api_name="/answer_question"
14
+ )
15
+ return result
16
 
17
+ # ChatInterface ve resim işleme fonksiyonu
18
  def respond(
19
  message,
20
  history: list[tuple[str, str]],
 
22
  max_tokens,
23
  temperature,
24
  top_p,
25
+ image
26
  ):
27
+ # Sistem mesajını ve önceki sohbeti al
28
  messages = [{"role": "system", "content": system_message}]
29
+
30
  for val in history:
31
  if val[0]:
32
  messages.append({"role": "user", "content": val[0]})
33
  if val[1]:
34
  messages.append({"role": "assistant", "content": val[1]})
35
 
36
+ # Resim ile ilgili açıklamayı ekle
37
+ image_description = describe_image(image)
38
+ messages.append({"role": "assistant", "content": image_description})
39
+
40
+ # Kullanıcı mesajını ekle
41
  messages.append({"role": "user", "content": message})
42
 
43
  response = ""
44
+ # Modelden gelen yanıtı al ve döngüde token'ları birleştir
45
  for message in client.chat_completion(
46
  messages,
47
  max_tokens=max_tokens,
 
50
  top_p=top_p,
51
  ):
52
  token = message.choices[0].delta.content
 
53
  response += token
54
  yield response
55
 
56
+ # Gradio app arayüzünü oluştur
57
+ demo = gr.Interface(
58
+ fn=respond,
59
+ inputs=[
 
 
 
60
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
61
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
62
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
63
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
64
+ gr.Image(type="pil", label="Upload an Image") # Resim girişi
 
 
 
 
 
65
  ],
66
+ outputs="text", # Çıktıyı metin olarak ver
67
  )
68
 
 
69
  if __name__ == "__main__":
70
  demo.launch()