Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,112 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
def
|
11 |
-
|
12 |
-
history:
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
|
|
|
|
27 |
|
28 |
-
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
|
|
|
|
|
|
40 |
yield response
|
|
|
|
|
|
|
|
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
"""
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
demo = gr.ChatInterface(
|
46 |
respond,
|
47 |
additional_inputs=[
|
48 |
-
gr.Textbox(value=
|
49 |
-
gr.
|
50 |
-
gr.
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
)
|
60 |
|
61 |
-
|
62 |
if __name__ == "__main__":
|
63 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from duckduckgo_search import DDGS
|
3 |
+
from collections import deque
|
4 |
+
import time
|
5 |
+
import random
|
|
|
|
|
6 |
|
7 |
+
def get_llm_response(prompt, model, max_retries=3):
|
8 |
+
for attempt in range(max_retries):
|
9 |
+
try:
|
10 |
+
return DDGS().chat(prompt, model=model)
|
11 |
+
except Exception as e:
|
12 |
+
if attempt < max_retries - 1:
|
13 |
+
print(f"Error occurred: {e}. Retrying in {2**attempt} seconds...")
|
14 |
+
time.sleep(2**attempt + random.random())
|
15 |
+
else:
|
16 |
+
print(f"Max retries reached. Error: {e}")
|
17 |
+
return f"<error>Unable to get response from {model} after {max_retries} attempts.</error>"
|
18 |
|
19 |
+
def process_message(message, history, analysis_prompt, rethinking_prompt, refinement_prompt):
|
20 |
+
conversation_history = deque(maxlen=5)
|
21 |
+
for h in history:
|
22 |
+
conversation_history.append(f"User: {h[0]}\nEcho-Refraction: {h[1]}")
|
23 |
+
|
24 |
+
context = "\n".join(conversation_history)
|
25 |
+
|
26 |
+
gpt4o_prompt = f"{analysis_prompt}\n\nConversation history:\n{context}\n\nUser query: {message}\n\nPlease analyze this query and respond accordingly."
|
27 |
+
gpt4o_response = get_llm_response(gpt4o_prompt, "gpt-4o-mini")
|
28 |
+
yield f"Analysis: {gpt4o_response}"
|
29 |
|
30 |
+
if "<error>" in gpt4o_response:
|
31 |
+
return
|
|
|
|
|
|
|
32 |
|
33 |
+
llama_prompt = f"{rethinking_prompt}\n\nConversation history:\n{context}\n\nOriginal user query: {message}\n\nInitial response: {gpt4o_response}\n\nPlease review and suggest improvements or confirm if satisfactory."
|
34 |
+
llama_response = get_llm_response(llama_prompt, "gpt-4o-mini")
|
35 |
+
yield f"Analysis: {gpt4o_response}\nRethinking: {llama_response}"
|
36 |
|
37 |
+
if "<error>" in llama_response:
|
38 |
+
return gpt4o_response
|
39 |
|
40 |
+
if "done" not in llama_response.lower():
|
41 |
+
final_gpt4o_prompt = f"{refinement_prompt}\n\nConversation history:\n{context}\n\nOriginal user query: {message}\n\nInitial response: {gpt4o_response}\n\nSuggestion: {llama_response}\n\nPlease provide a final response considering the suggestion."
|
42 |
+
final_response = get_llm_response(final_gpt4o_prompt, "gpt-4o-mini")
|
43 |
+
yield f"Analysis: {gpt4o_response}\nRethinking: {llama_response}\nFinal Response: {final_response}"
|
44 |
+
return final_response
|
45 |
+
else:
|
46 |
+
return gpt4o_response
|
|
|
47 |
|
48 |
+
def respond(message, history, analysis_prompt, rethinking_prompt, refinement_prompt):
|
49 |
+
response = ""
|
50 |
+
for chunk in process_message(message, history, analysis_prompt, rethinking_prompt, refinement_prompt):
|
51 |
+
response = chunk
|
52 |
yield response
|
53 |
+
|
54 |
+
# Extract the final response from the last chunk
|
55 |
+
final_response = response.split("Final Response: ")[-1] if "Final Response: " in response else response
|
56 |
+
return final_response
|
57 |
|
58 |
+
analysis_prompt = """
|
59 |
+
You are Echo-Refraction, an AI assistant tasked with analyzing user queries. Your role is to:
|
60 |
+
1. Carefully examine the user's input for clarity, completeness, and potential ambiguities.
|
61 |
+
2. Identify if the query needs refinement or additional information.
|
62 |
+
3. If refinement is needed, suggest specific improvements or ask clarifying questions.
|
63 |
+
4. If the query is clear, respond with "Query is clear and ready for processing."
|
64 |
+
5. Provide a brief explanation of your analysis in all cases.
|
65 |
+
Enclose your response in <analyzing> tags.
|
66 |
"""
|
67 |
+
|
68 |
+
rethinking_prompt = """
|
69 |
+
You are Echo-Refraction, an advanced AI model responsible for critically evaluating and improving responses. Your task is to:
|
70 |
+
1. Carefully review the original user query and the initial response.
|
71 |
+
2. Analyze the response for accuracy, relevance, completeness, and potential improvements.
|
72 |
+
3. Consider perspectives or approaches that might enhance the response.
|
73 |
+
4. If you identify areas for improvement:
|
74 |
+
a. Clearly explain what aspects need refinement and why.
|
75 |
+
b. Provide specific suggestions for how the response could be enhanced.
|
76 |
+
c. If necessary, propose additional information or context that could be included.
|
77 |
+
5. If the initial response is satisfactory and you have no suggestions for improvement, respond with "Done."
|
78 |
+
Enclose your response in <rethinking> tags.
|
79 |
"""
|
80 |
+
|
81 |
+
refinement_prompt = """
|
82 |
+
You are Echo-Refraction, an AI assistant tasked with providing a final, refined response to the user. Your role is to:
|
83 |
+
1. Review the original user query, your initial response, and the suggestions provided.
|
84 |
+
2. Consider the feedback and suggestions for improvement.
|
85 |
+
3. Integrate the suggested improvements into your response, ensuring that:
|
86 |
+
a. The information is accurate and up-to-date.
|
87 |
+
b. The response is comprehensive and addresses all aspects of the user's query.
|
88 |
+
c. The language is clear, concise, and appropriate for the user's level of understanding.
|
89 |
+
4. If you disagree with any suggestions, provide a brief explanation of why you chose not to incorporate them.
|
90 |
+
5. Deliver a final response that represents the best possible answer to the user's query.
|
91 |
+
Enclose your response in <output> tags.
|
92 |
+
"""
|
93 |
+
|
94 |
demo = gr.ChatInterface(
|
95 |
respond,
|
96 |
additional_inputs=[
|
97 |
+
gr.Textbox(value=analysis_prompt, label="Analysis Prompt", lines=10),
|
98 |
+
gr.Textbox(value=rethinking_prompt, label="Rethinking Prompt", lines=10),
|
99 |
+
gr.Textbox(value=refinement_prompt, label="Refinement Prompt", lines=10),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
],
|
101 |
+
title="Echo-Refraction AI Assistant",
|
102 |
+
description="Chat with Echo-Refraction, an AI assistant that analyzes, rethinks, and refines responses.",
|
103 |
+
examples=[
|
104 |
+
["How many 'r' are there in the word 'strawberry'"],
|
105 |
+
["Explain the concept of quantum entanglement."],
|
106 |
+
["How does photosynthesis work?"],
|
107 |
+
],
|
108 |
+
cache_examples=False,
|
109 |
)
|
110 |
|
|
|
111 |
if __name__ == "__main__":
|
112 |
demo.launch()
|