Update app.py
Browse files
app.py
CHANGED
|
@@ -14,13 +14,11 @@ import numpy as np
|
|
| 14 |
import ast
|
| 15 |
import operator as op
|
| 16 |
|
| 17 |
-
# === Setup ===
|
| 18 |
logging.basicConfig(level=logging.INFO)
|
| 19 |
logger = logging.getLogger(__name__)
|
| 20 |
-
|
| 21 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 22 |
|
| 23 |
-
|
| 24 |
llm = HuggingFaceLLM(
|
| 25 |
context_window=4096,
|
| 26 |
max_new_tokens=512,
|
|
@@ -29,7 +27,7 @@ llm = HuggingFaceLLM(
|
|
| 29 |
model_name="Gensyn/Qwen2.5-0.5B-Instruct",
|
| 30 |
)
|
| 31 |
|
| 32 |
-
|
| 33 |
class QuestionValidation:
|
| 34 |
def __init__(self, llm_client):
|
| 35 |
self.client = llm_client
|
|
@@ -51,7 +49,7 @@ class QuestionValidation:
|
|
| 51 |
"similarity": round(float(similarity), 4)
|
| 52 |
}
|
| 53 |
|
| 54 |
-
|
| 55 |
def search_web(query: str, max_results: int = 5) -> List[Dict[str, str]]:
|
| 56 |
try:
|
| 57 |
with DDGS() as ddgs:
|
|
@@ -89,7 +87,7 @@ def evaluate_math_expression(expr: str) -> str:
|
|
| 89 |
except Exception as e:
|
| 90 |
return f"Error evaluating expression: {e}"
|
| 91 |
|
| 92 |
-
|
| 93 |
validator = QuestionValidation(llm)
|
| 94 |
|
| 95 |
validate_tool = FunctionTool.from_defaults(
|
|
@@ -119,47 +117,17 @@ agent = ReActAgent.from_tools(
|
|
| 119 |
max_iterations=3
|
| 120 |
)
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
similarity_score = 0.0
|
| 126 |
-
retry = 0
|
| 127 |
-
max_retries = 2
|
| 128 |
-
|
| 129 |
-
while retry < max_retries:
|
| 130 |
-
guessed_question = validator.guess_question(llm_answer)
|
| 131 |
-
similarity_score = validator.compute_similarity(user_question, guessed_question)
|
| 132 |
-
if similarity_score > 0.6:
|
| 133 |
-
break
|
| 134 |
-
retry += 1
|
| 135 |
-
|
| 136 |
-
return (
|
| 137 |
-
f"Original Question: {user_question}\n\n"
|
| 138 |
-
f"Answer: {llm_answer}\n\n"
|
| 139 |
-
f"Guessed Question: {guessed_question}\n\n"
|
| 140 |
-
f"Similarity Score: {similarity_score:.4f}"
|
| 141 |
-
)
|
| 142 |
|
| 143 |
-
# === Gradio Interfaces ===
|
| 144 |
with gr.Blocks() as app:
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
description="Loops until the guessed question has a similarity score > 0.6."
|
| 152 |
-
).render()
|
| 153 |
-
|
| 154 |
-
gr.ChatInterface(
|
| 155 |
-
respond,
|
| 156 |
-
additional_inputs=[
|
| 157 |
-
gr.Textbox(value="You are a Chatbot.", label="System message"),
|
| 158 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 159 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 160 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
|
| 161 |
-
],
|
| 162 |
-
).render()
|
| 163 |
|
| 164 |
if __name__ == "__main__":
|
| 165 |
app.launch()
|
|
|
|
| 14 |
import ast
|
| 15 |
import operator as op
|
| 16 |
|
|
|
|
| 17 |
logging.basicConfig(level=logging.INFO)
|
| 18 |
logger = logging.getLogger(__name__)
|
|
|
|
| 19 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 20 |
|
| 21 |
+
|
| 22 |
llm = HuggingFaceLLM(
|
| 23 |
context_window=4096,
|
| 24 |
max_new_tokens=512,
|
|
|
|
| 27 |
model_name="Gensyn/Qwen2.5-0.5B-Instruct",
|
| 28 |
)
|
| 29 |
|
| 30 |
+
|
| 31 |
class QuestionValidation:
|
| 32 |
def __init__(self, llm_client):
|
| 33 |
self.client = llm_client
|
|
|
|
| 49 |
"similarity": round(float(similarity), 4)
|
| 50 |
}
|
| 51 |
|
| 52 |
+
|
| 53 |
def search_web(query: str, max_results: int = 5) -> List[Dict[str, str]]:
|
| 54 |
try:
|
| 55 |
with DDGS() as ddgs:
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
return f"Error evaluating expression: {e}"
|
| 89 |
|
| 90 |
+
|
| 91 |
validator = QuestionValidation(llm)
|
| 92 |
|
| 93 |
validate_tool = FunctionTool.from_defaults(
|
|
|
|
| 117 |
max_iterations=3
|
| 118 |
)
|
| 119 |
|
| 120 |
+
def respond(message: str, history: List[List[str]]) -> str:
|
| 121 |
+
response = agent.chat(message)
|
| 122 |
+
return response.response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
|
|
|
| 124 |
with gr.Blocks() as app:
|
| 125 |
+
gr.ChatInterface(
|
| 126 |
+
respond,
|
| 127 |
+
chatbot=gr.Chatbot(),
|
| 128 |
+
title="Smart Assistant",
|
| 129 |
+
description="Ask me anything — math, web search, or validating a question.",
|
| 130 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
if __name__ == "__main__":
|
| 133 |
app.launch()
|