Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message,
|
|
@@ -24,7 +145,6 @@ def respond(
|
|
| 24 |
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
response = ""
|
| 29 |
|
| 30 |
for message in client.chat_completion(
|
|
@@ -35,30 +155,19 @@ def respond(
|
|
| 35 |
top_p=top_p,
|
| 36 |
):
|
| 37 |
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
|
|
|
|
|
|
| 1 |
+
# TOOLS
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
import gradio as gr
|
| 5 |
+
import logging
|
| 6 |
+
from typing import Dict, List
|
| 7 |
+
|
| 8 |
from huggingface_hub import InferenceClient
|
| 9 |
+
from llama_index.core.tools import FunctionTool
|
| 10 |
+
from duckduckgo_search import DDGS
|
| 11 |
+
from sentence_transformers import SentenceTransformer
|
| 12 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 13 |
+
import numpy as np
|
| 14 |
|
| 15 |
+
# Load HF_TOKEN from environment
|
| 16 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 17 |
+
|
| 18 |
+
logging.basicConfig(level=logging.INFO)
|
| 19 |
+
logger = logging.getLogger(__name__)
|
| 20 |
+
|
| 21 |
+
# === Core Components ===
|
| 22 |
+
class QuestionValidation:
|
| 23 |
+
def __init__(self, hf_token: str):
|
| 24 |
+
self.client = InferenceClient(provider="hf-inference", api_key=hf_token)
|
| 25 |
+
self.llm2_model = "HuggingFaceH4/zephyr-7b-beta"
|
| 26 |
+
self.embedding_model = SentenceTransformer("sentence-transformers/all-mpnet-base-v2")
|
| 27 |
+
|
| 28 |
+
def guess_question(self, answer: str) -> str:
|
| 29 |
+
prompt = f"This was the answer: {answer}\nWhat question would likely have led to it?"
|
| 30 |
+
response = self.client.text_generation(prompt=prompt, model=self.llm2_model, max_new_tokens=100)
|
| 31 |
+
return response
|
| 32 |
+
|
| 33 |
+
def compute_similarity(self, q1: str, q2: str) -> float:
|
| 34 |
+
embeddings = self.embedding_model.encode([q1, q2])
|
| 35 |
+
return cosine_similarity([embeddings[0]], [embeddings[1]])[0][0]
|
| 36 |
+
|
| 37 |
+
def validate_question_only(self, original_question: str, guessed_question: str) -> Dict[str, float]:
|
| 38 |
+
similarity = self.compute_similarity(original_question, guessed_question)
|
| 39 |
+
return {
|
| 40 |
+
"original_question": original_question,
|
| 41 |
+
"guessed_question": guessed_question,
|
| 42 |
+
"similarity": round(float(similarity), 4)
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
def search_web(query: str, max_results: int = 5) -> List[Dict[str, str]]:
|
| 46 |
+
try:
|
| 47 |
+
with DDGS() as ddgs:
|
| 48 |
+
results = [r for r in ddgs.text(query, max_results=max_results)]
|
| 49 |
+
return results
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return [{"error": str(e)}]
|
| 52 |
+
|
| 53 |
+
def evaluate_math_expression(expr: str) -> str:
|
| 54 |
+
try:
|
| 55 |
+
result = eval(expr, {"__builtins__": {}})
|
| 56 |
+
return str(result)
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return f"Error evaluating expression: {e}"
|
| 59 |
+
|
| 60 |
+
validator = QuestionValidation(hf_token=HF_TOKEN)
|
| 61 |
+
|
| 62 |
+
validate_tool = FunctionTool.from_defaults(
|
| 63 |
+
fn=validator.validate_question_only,
|
| 64 |
+
name="validate_question",
|
| 65 |
+
description="Compares the similarity between two questions."
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
search_tool = FunctionTool.from_defaults(
|
| 69 |
+
fn=search_web,
|
| 70 |
+
name="search_web",
|
| 71 |
+
description="Searches the web using DuckDuckGo and returns results."
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
math_tool = FunctionTool.from_defaults(
|
| 75 |
+
fn=evaluate_math_expression,
|
| 76 |
+
name="math_tool",
|
| 77 |
+
description="Evaluates a basic Python math expression."
|
| 78 |
+
)
|
| 79 |
|
| 80 |
+
TOOLS = [validate_tool, search_tool, math_tool]
|
| 81 |
+
|
| 82 |
+
from llama_index.core.agent import ReActAgent
|
| 83 |
+
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
| 84 |
+
|
| 85 |
+
llm = HuggingFaceInferenceAPI(
|
| 86 |
+
model_name="mistralai/Mistral-7B-Instruct-v0.3",
|
| 87 |
+
token=HF_TOKEN,
|
| 88 |
+
context_window=3900,
|
| 89 |
+
max_new_tokens=256,
|
| 90 |
+
generate_kwargs={"temperature": 0.7}
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
agent = ReActAgent.from_tools(
|
| 94 |
+
tools=TOOLS,
|
| 95 |
+
llm=llm,
|
| 96 |
+
verbose=True,
|
| 97 |
+
max_iterations=3
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
def question_loop_agent(user_question: str):
|
| 101 |
+
llm_answer = llm.complete(user_question).text.strip()
|
| 102 |
+
similarity_score = 0.0
|
| 103 |
+
retry = 0
|
| 104 |
+
max_retries = 5
|
| 105 |
+
|
| 106 |
+
while retry < max_retries:
|
| 107 |
+
guessed_question = validator.guess_question(llm_answer)
|
| 108 |
+
similarity_score = validator.compute_similarity(user_question, guessed_question)
|
| 109 |
+
if similarity_score > 0.6:
|
| 110 |
+
break
|
| 111 |
+
retry += 1
|
| 112 |
+
|
| 113 |
+
return (
|
| 114 |
+
f"Original Question: {user_question}\n\n"
|
| 115 |
+
f"Answer: {llm_answer}\n\n"
|
| 116 |
+
f"Guessed Question: {guessed_question}\n\n"
|
| 117 |
+
f"Similarity Score: {similarity_score:.4f}"
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
iface_loop = gr.Interface(
|
| 121 |
+
fn=question_loop_agent,
|
| 122 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask me a question..."),
|
| 123 |
+
outputs="text",
|
| 124 |
+
title="🧠 Question Similarity Loop Agent",
|
| 125 |
+
description="Loops until the guessed question has a similarity score > 0.6."
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# --- Additional ChatInterface Template ---
|
| 129 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 130 |
|
| 131 |
def respond(
|
| 132 |
message,
|
|
|
|
| 145 |
messages.append({"role": "assistant", "content": val[1]})
|
| 146 |
|
| 147 |
messages.append({"role": "user", "content": message})
|
|
|
|
| 148 |
response = ""
|
| 149 |
|
| 150 |
for message in client.chat_completion(
|
|
|
|
| 155 |
top_p=top_p,
|
| 156 |
):
|
| 157 |
token = message.choices[0].delta.content
|
|
|
|
| 158 |
response += token
|
| 159 |
yield response
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
demo = gr.ChatInterface(
|
| 162 |
respond,
|
| 163 |
additional_inputs=[
|
| 164 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 165 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 166 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 167 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
],
|
| 169 |
)
|
| 170 |
|
|
|
|
| 171 |
if __name__ == "__main__":
|
| 172 |
+
iface_loop.launch()
|
| 173 |
+
# demo.launch() # Uncomment to run the chat template too
|