rafaldembski
commited on
Commit
•
942ded2
1
Parent(s):
12c5c22
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,10 @@ import openai
|
|
3 |
import time
|
4 |
import re
|
5 |
import os
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Available models
|
8 |
MODELS = [
|
@@ -14,6 +18,12 @@ MODELS = [
|
|
14 |
# Sambanova API base URL
|
15 |
API_BASE = "https://api.sambanova.ai/v1"
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
def create_client(api_key=None):
|
18 |
"""Creates an OpenAI client instance."""
|
19 |
if api_key:
|
@@ -34,6 +44,24 @@ def chat_with_ai(message, chat_history, system_prompt):
|
|
34 |
messages.append({"role": "user", "content": message})
|
35 |
return messages
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
def respond(message, chat_history, model, system_prompt, thinking_budget, api_key):
|
38 |
"""Sends the message to the API and gets the response."""
|
39 |
client = create_client(api_key)
|
@@ -95,69 +123,15 @@ You are D-LOGIC, an advanced AI assistant created by Rafał Dembski, a passionat
|
|
95 |
- **Engaging and Interactive**: Maintain an engaging conversation, using humor, interactive features (e.g., quizzes, polls), and emotional intelligence.
|
96 |
- **Emotionally Adapted**: Analyze the user's emotional tone and adjust responses with empathy and appropriateness.
|
97 |
- **Error-Free and Well-Formatted**: Ensure clarity and correctness in all communications, using structured formats such as headings, bullet points, and clear sections.
|
98 |
-
|
99 |
-
### **Advanced Thinking Mechanism**:
|
100 |
-
|
101 |
-
To provide the most comprehensive and well-thought-out answers, follow this enhanced thought process. Use **visual formatting** like **bold text**, *italics*, bullet points, headers, and appropriate use of emoticons to make the responses engaging and easy to read.
|
102 |
-
|
103 |
-
1. **Understand the Question**:
|
104 |
-
- **Context Analysis**: Carefully read the user’s message to fully grasp the intent, emotions, and context.
|
105 |
-
- **Identify Key Elements**: Break down the question into its essential components that require detailed analysis.
|
106 |
-
|
107 |
-
2. **Set Thinking Budget**:
|
108 |
-
- **Expanded Budget**: Set a limit of 25 steps to allow for deeper analysis and reflection.
|
109 |
-
- Track each step, making sure to stay within the allocated budget. If necessary, reflect on the remaining steps to ensure efficient thinking.
|
110 |
-
|
111 |
-
3. **Step-by-Step Breakdown**:
|
112 |
-
- **Step 1: Define the Problem** 🧐 – Clearly identify the core issue or request.
|
113 |
-
- **Step 2: Data Gathering** 📊 – Gather relevant information from your knowledge base or external tools if allowed.
|
114 |
-
- **Step 3: Data Analysis** 🔍 – Analyze the gathered data critically to extract meaningful insights.
|
115 |
-
- **Step 4: Explore Alternatives** 🔄 – Consider multiple perspectives and possible solutions. Always provide at least two alternatives.
|
116 |
-
- **Step 5: Select the Best Solution** 🏆 – Choose the most logical and appropriate solution based on the available information.
|
117 |
-
- **Step 6: Plan Action** 📝 – Determine the necessary steps to implement the solution effectively.
|
118 |
-
- **Step 7: Predict Consequences** 🔮 – Consider possible outcomes and consequences of implementing the solution.
|
119 |
-
- **Step 8: Self-Reflection** 🤔 – Reflect on the thought process up to this point. Are there any gaps or areas that could be improved?
|
120 |
-
- **Step 9: Formulate the Final Answer** ✍️ – Synthesize the information and insights into a coherent and clear response.
|
121 |
-
- **Step 10: Reflection** 💡 – Evaluate the overall process, analyzing how well the response meets the user's needs.
|
122 |
-
|
123 |
-
4. **Reflection and Self-Evaluation**:
|
124 |
-
- **Reflection after Each Step**: After each step, reflect on the process and make adjustments if needed.
|
125 |
-
- **Final Reflection**: Provide a critical, honest evaluation of the entire process and the solution provided.
|
126 |
-
- **Assign a Quality Score**: Assign a score between 0.0 (lowest) and 1.0 (highest) for the quality of the answer. Be honest and objective about the score.
|
127 |
-
|
128 |
-
5. **Final Answer**:
|
129 |
-
- **Answer Summary**: Provide a well-structured final answer, synthesizing all steps in a clear, concise format.
|
130 |
-
- **Visual Formatting**: Use **bold text**, *italics*, lists, or quotes to make the answer visually appealing and easy to read.
|
131 |
-
- **Strive for Excellence**: Always aim for the highest standard in every response, ensuring it is both informative and engaging. **Don't forget to use emoticons** to improve readability and engagement where appropriate (e.g., 😊, 🤔, ✅, 🏆).
|
132 |
-
|
133 |
-
### Example Interaction Structure:
|
134 |
-
|
135 |
-
1. **Greeting**:
|
136 |
-
- **"Hello! 👋 How can I assist you today?"**
|
137 |
-
|
138 |
-
2. **Mood Check**:
|
139 |
-
- *"How are you feeling today? 😊 Is there anything I can do to brighten your mood?"*
|
140 |
-
|
141 |
-
3. **Interactive Engagement**:
|
142 |
-
- *"Here are a few things you can ask me about: weather 🌦️, technology news 🖥️, health advice 🏋️, or even send me a document for analysis."*
|
143 |
-
|
144 |
-
4. **Engagement Option**:
|
145 |
-
- *"Would you like to try a quick quiz, or maybe analyze a document 📄 for more details?"*
|
146 |
-
|
147 |
-
5. **Closing**:
|
148 |
-
- *"Thank you for the conversation! 😊 Is there anything else I can help you with?"*
|
149 |
-
|
150 |
-
### **Critical Self-Evaluation**:
|
151 |
-
- **Krytyczna ocena**: Po zakończeniu odpowiedzi, asystent musi ocenić swoje działania. Jak mógłbym to poprawić następnym razem? Czy wszystkie kroki były wykonane w najbardziej efektywny sposób? Jakie wnioski mogę wyciągnąć na przyszłość?
|
152 |
"""
|
153 |
|
154 |
-
#
|
155 |
|
156 |
with gr.Blocks() as demo:
|
157 |
# New header and description for D-LOGIC
|
158 |
gr.Markdown("# D-LOGIC: Twój Inteligentny Asystent AI")
|
159 |
gr.Markdown("""
|
160 |
-
**D-LOGIC** to zaawansowany asystent AI stworzony przez Rafała Dembskiego. Pomaga w rozwiązywaniu problemów, analizie dokumentów i oferuje spersonalizowane odpowiedzi, dostosowane do Twoich emocji i potrzeb.
|
161 |
""")
|
162 |
|
163 |
with gr.Row():
|
@@ -165,16 +139,23 @@ with gr.Blocks() as demo:
|
|
165 |
thinking_budget = gr.Slider(minimum=1, maximum=100, value=25, step=1, label="Budżet Myślenia", info="Maksymalna liczba kroków, które model może przemyśleć")
|
166 |
|
167 |
chatbot = gr.Chatbot(label="Chat", show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel", type="messages")
|
168 |
-
|
169 |
-
|
|
|
|
|
170 |
|
171 |
submit_button = gr.Button("Wyślij")
|
172 |
clear_button = gr.Button("Wyczyść Chat")
|
173 |
|
174 |
clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, msg])
|
175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
# Submit messages by pressing Enter or clicking the Submit button
|
177 |
-
|
178 |
-
submit_button.click(generate, inputs=[msg, chatbot, model, thinking_budget], outputs=[chatbot, msg])
|
179 |
|
180 |
demo.launch(share=True, show_api=False)
|
|
|
3 |
import time
|
4 |
import re
|
5 |
import os
|
6 |
+
from PIL import Image
|
7 |
+
from transformers import LlavaProcessor, LlavaForConditionalGeneration, TextIteratorStreamer
|
8 |
+
from threading import Thread
|
9 |
+
import torch
|
10 |
|
11 |
# Available models
|
12 |
MODELS = [
|
|
|
18 |
# Sambanova API base URL
|
19 |
API_BASE = "https://api.sambanova.ai/v1"
|
20 |
|
21 |
+
# Load image processing model
|
22 |
+
model_id = "llava-hf/llava-interleave-qwen-0.5b-hf"
|
23 |
+
processor = LlavaProcessor.from_pretrained(model_id)
|
24 |
+
model = LlavaForConditionalGeneration.from_pretrained(model_id)
|
25 |
+
model.to("cpu")
|
26 |
+
|
27 |
def create_client(api_key=None):
|
28 |
"""Creates an OpenAI client instance."""
|
29 |
if api_key:
|
|
|
44 |
messages.append({"role": "user", "content": message})
|
45 |
return messages
|
46 |
|
47 |
+
def llava_image_processing(image, prompt):
|
48 |
+
"""Processes the image using the Llava model."""
|
49 |
+
gr.Info("Analyzing image")
|
50 |
+
image = Image.open(image).convert("RGB")
|
51 |
+
formatted_prompt = f"<|im_start|>user <image>\n{prompt}<|im_end|><|im_start|>assistant"
|
52 |
+
|
53 |
+
inputs = processor(formatted_prompt, image, return_tensors="pt")
|
54 |
+
streamer = TextIteratorStreamer(processor, skip_prompt=True, **{"skip_special_tokens": True})
|
55 |
+
generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=1024)
|
56 |
+
|
57 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
58 |
+
thread.start()
|
59 |
+
|
60 |
+
buffer = ""
|
61 |
+
for new_text in streamer:
|
62 |
+
buffer += new_text
|
63 |
+
yield buffer
|
64 |
+
|
65 |
def respond(message, chat_history, model, system_prompt, thinking_budget, api_key):
|
66 |
"""Sends the message to the API and gets the response."""
|
67 |
client = create_client(api_key)
|
|
|
123 |
- **Engaging and Interactive**: Maintain an engaging conversation, using humor, interactive features (e.g., quizzes, polls), and emotional intelligence.
|
124 |
- **Emotionally Adapted**: Analyze the user's emotional tone and adjust responses with empathy and appropriateness.
|
125 |
- **Error-Free and Well-Formatted**: Ensure clarity and correctness in all communications, using structured formats such as headings, bullet points, and clear sections.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
"""
|
127 |
|
128 |
+
# Updated interface with image analysis capability
|
129 |
|
130 |
with gr.Blocks() as demo:
|
131 |
# New header and description for D-LOGIC
|
132 |
gr.Markdown("# D-LOGIC: Twój Inteligentny Asystent AI")
|
133 |
gr.Markdown("""
|
134 |
+
**D-LOGIC** to zaawansowany asystent AI stworzony przez Rafała Dembskiego. Pomaga w rozwiązywaniu problemów, analizie dokumentów i oferuje spersonalizowane odpowiedzi, dostosowane do Twoich emocji i potrzeb. Możesz także przesłać obraz do analizy!
|
135 |
""")
|
136 |
|
137 |
with gr.Row():
|
|
|
139 |
thinking_budget = gr.Slider(minimum=1, maximum=100, value=25, step=1, label="Budżet Myślenia", info="Maksymalna liczba kroków, które model może przemyśleć")
|
140 |
|
141 |
chatbot = gr.Chatbot(label="Chat", show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel", type="messages")
|
142 |
+
|
143 |
+
with gr.Row():
|
144 |
+
msg = gr.Textbox(label="Wpisz swoją wiadomość...", placeholder="Wprowadź swoją wiadomość...")
|
145 |
+
image_input = gr.File(label="Prześlij obraz do analizy (opcjonalnie)")
|
146 |
|
147 |
submit_button = gr.Button("Wyślij")
|
148 |
clear_button = gr.Button("Wyczyść Chat")
|
149 |
|
150 |
clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, msg])
|
151 |
|
152 |
+
def handle_message_or_image(message, image, chatbot, model, thinking_budget):
|
153 |
+
if image:
|
154 |
+
return llava_image_processing(image, message), ""
|
155 |
+
else:
|
156 |
+
return generate(message, chatbot, model, thinking_budget)
|
157 |
+
|
158 |
# Submit messages by pressing Enter or clicking the Submit button
|
159 |
+
submit_button.click(fn=handle_message_or_image, inputs=[msg, image_input, chatbot, model, thinking_budget], outputs=[chatbot, msg])
|
|
|
160 |
|
161 |
demo.launch(share=True, show_api=False)
|