suayptalha commited on
Commit
7b6d332
·
verified ·
1 Parent(s): 925d9fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -72
app.py CHANGED
@@ -2,91 +2,72 @@ import gradio as gr
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şi
12
- history = []
13
 
14
- # Resim açıklama fonksiyonu
15
- def describe_image(image, user_message, history):
16
- # Resmi Moondream2 API'sine gönderiyoruz
17
- result = moondream_client.predict(
18
- img=handle_file(image),
 
 
 
 
 
 
 
 
19
  prompt="Describe this image.",
20
  api_name="/answer_question"
21
  )
22
 
23
- description = result # Moondream2'den açıklama alıyoruz
24
- history.append({"role": "user", "content": user_message}) # string olarak
25
- history.append({"role": "assistant", "content": description}) # string olarak
26
-
27
- # Resim açıklamasına alt metin (alt_text) ekliyoruz
28
- image_message = {
29
- "type": "image",
30
- "data": image,
31
- "alt_text": description # Resmin açıklaması olarak alt metni ekliyoruz
32
- }
33
 
34
- return description, history, image_message
35
-
36
- # Text ve history ile sohbet fonksiyonu
37
- def chat_with_text(user_message, history, max_new_tokens=250):
38
- # Kullanıcı mesajını history'ye ekliyoruz
39
- history.append({"role": "user", "content": user_message}) # string olarak
40
-
41
- # Tüm geçmişi LLaMA'ya gönderiyoruz
42
- texts = [{"role": msg["role"], "content": msg["content"]} for msg in history]
43
- llama_result = llama_client.chat_completion(
44
- messages=texts,
45
- max_tokens=max_new_tokens,
46
- temperature=0.7,
47
- top_p=0.95
48
- )
49
-
50
- # Asistan cevabını alıyoruz ve history'ye ekliyoruz
51
- assistant_reply = llama_result["choices"][0]["message"]["content"]
52
- history.append({"role": "assistant", "content": assistant_reply}) # string olarak
53
-
54
- return assistant_reply, history
55
 
56
- # Resim ve/veya metin tabanlı sohbet fonksiyonu
57
- def bot_streaming(message, history=None, max_new_tokens=250):
58
- if history is None: # Eğer `history` verilmemişse boş bir liste kullanıyoruz
59
- history = []
60
-
61
- user_message = message.get("text", "")
62
- image = message.get("image", None)
63
-
64
- if image: # Resim varsa
65
- response, history, image_message = describe_image(image, user_message, history)
66
- return image_message, history # Resim ve tarihçeyi döndürüyoruz
67
- else: # Sadece metin mesajı varsa
68
- response, history = chat_with_text(user_message, history, max_new_tokens)
69
- return response, history # Yalnızca metin döndürülmeli
 
70
 
71
- # Gradio arayüzü
72
  demo = gr.ChatInterface(
73
- fn=bot_streaming, # Buradaki fonksiyon bot_streaming
74
- title="Multimodal Chat Assistant",
75
  additional_inputs=[
76
- gr.Textbox(value="You are a friendly assistant.", label="System message"),
77
- gr.Slider(minimum=10, maximum=500, value=250, step=10, label="Maximum number of new tokens to generate"),
78
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
79
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
 
80
  ],
81
- description=(
82
- "This demo combines text and image understanding using Moondream2 for visual "
83
- "tasks and LLaMA for conversational AI. Upload an image, ask questions, "
84
- "or just chat!"
85
- ),
86
- stop_btn="Stop Generation",
87
- fill_height=True,
88
- multimodal=True,
89
  )
90
 
91
  if __name__ == "__main__":
92
- demo.launch(debug=True)
 
2
  from gradio_client import Client, handle_file
3
  from huggingface_hub import InferenceClient
4
 
5
+ # Initialize the InferenceClient for FastLlama model
6
+ client = InferenceClient("Qwen/QwQ-32B-Preview")
 
 
 
7
 
8
+ # Initialize the Moondream Client for image description
9
+ moondream_client = Client("vikhyatk/moondream2")
10
 
11
+ def respond(
12
+ message,
13
+ history: list[tuple[str, str]],
14
+ system_message,
15
+ max_tokens,
16
+ temperature,
17
+ top_p,
18
+ image_input
19
+ ):
20
+ # Step 1: Handle the image and get its description using Moondream API
21
+ image_file = handle_file(image_input)
22
+ image_description = moondream_client.predict(
23
+ img=image_file,
24
  prompt="Describe this image.",
25
  api_name="/answer_question"
26
  )
27
 
28
+ # Step 2: Create the messages for the chat model
29
+ messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
 
 
30
 
31
+ # Add history to the messages
32
+ for val in history:
33
+ if val[0]:
34
+ messages.append({"role": "user", "content": val[0]})
35
+ if val[1]:
36
+ messages.append({"role": "assistant", "content": val[1]})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ # Add the image description to the user message
39
+ messages.append({"role": "user", "content": f"Here is the description of the image: {image_description}. Can you comment on it?"})
40
+
41
+ # Step 3: Get the response from the assistant
42
+ response = ""
43
+ for message in client.chat_completion(
44
+ messages,
45
+ max_tokens=max_tokens,
46
+ stream=True,
47
+ temperature=temperature,
48
+ top_p=top_p,
49
+ ):
50
+ token = message.choices[0].delta.content
51
+ response += token
52
+ yield response
53
 
54
+ # Set up Gradio interface
55
  demo = gr.ChatInterface(
56
+ respond,
 
57
  additional_inputs=[
58
+ gr.Textbox(value="You are a friendly assistant named FastLlama.", label="System message"),
59
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
60
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
61
+ gr.Slider(
62
+ minimum=0.1,
63
+ maximum=1.0,
64
+ value=0.95,
65
+ step=0.05,
66
+ label="Top-p (nucleus sampling)",
67
+ ),
68
+ gr.Image(type="pil", label="Upload Image for Description") # Image input
69
  ],
 
 
 
 
 
 
 
 
70
  )
71
 
72
  if __name__ == "__main__":
73
+ demo.launch()