suayptalha commited on
Commit
e719a30
·
verified ·
1 Parent(s): d2449dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -34
app.py CHANGED
@@ -2,62 +2,73 @@ import gradio as gr
2
  from gradio_client import Client, handle_file
3
  from huggingface_hub import InferenceClient
4
 
 
5
  moondream_client = Client("vikhyatk/moondream2")
 
 
6
  llama_client = InferenceClient("Qwen/QwQ-32B-Preview")
7
 
 
8
  history = []
9
 
 
10
  def describe_image(image, user_message):
11
  global history
 
 
12
  result = moondream_client.predict(
13
  img=handle_file(image),
14
  prompt="Describe this image.",
15
  api_name="/answer_question"
16
  )
17
 
18
- description = result
 
 
 
19
  history.append(f"User: {user_message}")
20
  history.append(f"Assistant: {description}")
21
 
 
22
  full_conversation = "\n".join(history)
23
- return respond(user_message, history, "System: Describe the image.", 512, 0.7, 0.95)
24
-
25
- def respond(message, history, system_message, max_tokens, temperature, top_p):
26
- messages = [{"role": "system", "content": system_message}]
27
-
28
- for val in history:
29
- if val[0]:
30
- messages.append({"role": "user", "content": val[0]})
31
- if val[1]:
32
- messages.append({"role": "assistant", "content": val[1]})
33
-
34
- messages.append({"role": "user", "content": message})
35
 
36
- response = ""
37
- for message in llama_client.chat_completion(
38
- messages,
39
- max_tokens=max_tokens,
40
- stream=True,
41
- temperature=temperature,
42
- top_p=top_p,
43
- ):
44
- token = message.choices[0].delta.content
45
- response += token
46
- yield response
47
 
 
48
  def chat_or_image(image, user_message):
49
  global history
 
 
50
  if image:
51
  return describe_image(image, user_message)
52
  else:
 
53
  history.append(f"User: {user_message}")
54
- return respond(user_message, history, "System: Answer the user's question.", 512, 0.7, 0.95)
55
-
56
- with gr.Blocks() as demo:
57
- with gr.Row():
58
- with gr.Column(scale=1):
59
- gr.Image(type="filepath", label="Upload Image (optional)", elem_id="left-column")
60
- with gr.Column(scale=2):
61
- gr.Textbox(label="Ask or Chat", placeholder="Ask a question...", lines=2, elem_id="right-column")
62
-
63
- demo.launch(show_error=True)
 
 
 
 
 
 
 
 
 
 
 
 
2
  from gradio_client import Client, handle_file
3
  from huggingface_hub import InferenceClient
4
 
5
+ # Moondream2 için Client kullanıyoruz
6
  moondream_client = Client("vikhyatk/moondream2")
7
+
8
+ # LLaMA için InferenceClient kullanıyoruz
9
  llama_client = InferenceClient("Qwen/QwQ-32B-Preview")
10
 
11
+ # Sohbet geçmişini tutmak için bir değişken
12
  history = []
13
 
14
+ # Resim açıklama fonksiyonu
15
  def describe_image(image, user_message):
16
  global history
17
+
18
+ # Resmi Moondream2 API'sine gönderiyoruz
19
  result = moondream_client.predict(
20
  img=handle_file(image),
21
  prompt="Describe this image.",
22
  api_name="/answer_question"
23
  )
24
 
25
+ # Moondream2'den alınan açıklamayı sisteme dahil ediyoruz
26
+ description = result # Moondream2'nin cevabını alıyoruz
27
+
28
+ # LLaMA API'sine açıklamayı ve kullanıcının mesajını gönderiyoruz
29
  history.append(f"User: {user_message}")
30
  history.append(f"Assistant: {description}")
31
 
32
+ # Sohbet geçmişini birleştirip tek bir mesaj olarak LLaMA'ya gönderiyoruz
33
  full_conversation = "\n".join(history)
34
+ llama_result = llama_client.chat_completion(
35
+ messages=[{"role": "user", "content": full_conversation}],
36
+ max_tokens=512, # Burada token sayısını belirleyebilirsiniz
37
+ temperature=0.7, # Sıcaklık parametresi
38
+ top_p=0.95 # Nucleus sampling için top_p parametresi
39
+ )
 
 
 
 
 
 
40
 
41
+ # Sonucu döndürüyoruz
42
+ return description + "\n\nAssistant: " + llama_result['choices'][0]['message']['content']
 
 
 
 
 
 
 
 
 
43
 
44
+ # Sohbet fonksiyonu, resim yüklenip yüklenmediğine göre yönlendirecek
45
  def chat_or_image(image, user_message):
46
  global history
47
+
48
+ # Resim yüklenmişse, önce açıklama alıp sonra LLaMA'ya gönderiyoruz
49
  if image:
50
  return describe_image(image, user_message)
51
  else:
52
+ # Resim yoksa, direkt LLaMA'ya mesajı gönderiyoruz
53
  history.append(f"User: {user_message}")
54
+ full_conversation = "\n".join(history)
55
+ llama_result = llama_client.chat_completion(
56
+ messages=[{"role": "user", "content": full_conversation}],
57
+ max_tokens=512,
58
+ temperature=0.7,
59
+ top_p=0.95
60
+ )
61
+ return llama_result['choices'][0]['message']['content']
62
+
63
+ # Gradio arayüzü
64
+ demo = gr.Interface(
65
+ fn=chat_or_image, # Hem resim hem de metin için kullanılacak fonksiyon
66
+ inputs=[
67
+ gr.Image(type="filepath", label="Upload image (Optional)"), # Resim yükleme
68
+ gr.Textbox(label="Ask anything", placeholder="Soru sor...", lines=2) # Metin girişi
69
+ ],
70
+ outputs="text", # Çıktı metin olarak dönecek
71
+ )
72
+
73
+ if _name_ == "_main_":
74
+ demo.launch(show_error=True) # Hata raporlamayı etkinleştiriyoruz