Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,44 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
from web import search #
|
| 4 |
|
| 5 |
client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
|
| 6 |
|
| 7 |
def is_uncertain(question, response):
|
| 8 |
"""Check if the model's response is unreliable."""
|
| 9 |
-
|
| 10 |
-
if len(response.split()) < 4:
|
| 11 |
return True
|
| 12 |
-
|
| 13 |
-
# 2. If response repeats the question, it might be unsure.
|
| 14 |
-
if response.lower() in question.lower():
|
| 15 |
return True
|
| 16 |
-
|
| 17 |
-
# 3. If the response contains generic phrases like "Kulingana na utafiti" (According to research)
|
| 18 |
uncertain_phrases = [
|
| 19 |
"Kulingana na utafiti", "Inaaminika kuwa", "Ninadhani",
|
| 20 |
"It is believed that", "Some people say", "Inasemekana kuwa"
|
| 21 |
]
|
| 22 |
if any(phrase.lower() in response.lower() for phrase in uncertain_phrases):
|
| 23 |
return True
|
| 24 |
-
|
| 25 |
return False
|
| 26 |
|
| 27 |
def google_search(query):
|
| 28 |
-
"""Fetch search results
|
| 29 |
-
results = search(query)
|
| 30 |
if results:
|
| 31 |
-
return results[0] # Return
|
| 32 |
return "Sorry, I couldn't find an answer on Google."
|
| 33 |
|
| 34 |
-
def respond(
|
| 35 |
-
message,
|
| 36 |
-
history: list[tuple[str, str]],
|
| 37 |
-
system_message,
|
| 38 |
-
max_tokens,
|
| 39 |
-
temperature,
|
| 40 |
-
top_p,
|
| 41 |
-
):
|
| 42 |
messages = [{"role": "system", "content": system_message}]
|
| 43 |
-
|
| 44 |
for val in history:
|
| 45 |
-
if val[0]:
|
| 46 |
-
|
| 47 |
-
if val[1]:
|
| 48 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 49 |
-
|
| 50 |
messages.append({"role": "user", "content": message})
|
| 51 |
|
| 52 |
response = ""
|
| 53 |
-
|
| 54 |
-
for message in client.chat_completion(
|
| 55 |
-
messages,
|
| 56 |
-
max_tokens=max_tokens,
|
| 57 |
-
stream=True,
|
| 58 |
-
temperature=temperature,
|
| 59 |
-
top_p=top_p,
|
| 60 |
-
):
|
| 61 |
token = message.choices[0].delta.content
|
| 62 |
response += token
|
| 63 |
-
yield response # Stream
|
| 64 |
-
|
| 65 |
-
# If response is unreliable, fetch from Google
|
| 66 |
if is_uncertain(message, response):
|
| 67 |
google_response = google_search(message)
|
| 68 |
yield f"🤖 AI: {response}\n\n🌍 Google: {google_response}"
|
|
@@ -73,13 +49,7 @@ demo = gr.ChatInterface(
|
|
| 73 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 74 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 75 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 76 |
-
gr.Slider(
|
| 77 |
-
minimum=0.1,
|
| 78 |
-
maximum=1.0,
|
| 79 |
-
value=0.95,
|
| 80 |
-
step=0.05,
|
| 81 |
-
label="Top-p (nucleus sampling)",
|
| 82 |
-
),
|
| 83 |
],
|
| 84 |
)
|
| 85 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
from web import search # This will now use the `web` tool correctly
|
| 4 |
|
| 5 |
client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
|
| 6 |
|
| 7 |
def is_uncertain(question, response):
|
| 8 |
"""Check if the model's response is unreliable."""
|
| 9 |
+
if len(response.split()) < 4: # Too short = likely incorrect
|
|
|
|
| 10 |
return True
|
| 11 |
+
if response.lower() in question.lower(): # Repeats question = unsure
|
|
|
|
|
|
|
| 12 |
return True
|
|
|
|
|
|
|
| 13 |
uncertain_phrases = [
|
| 14 |
"Kulingana na utafiti", "Inaaminika kuwa", "Ninadhani",
|
| 15 |
"It is believed that", "Some people say", "Inasemekana kuwa"
|
| 16 |
]
|
| 17 |
if any(phrase.lower() in response.lower() for phrase in uncertain_phrases):
|
| 18 |
return True
|
|
|
|
| 19 |
return False
|
| 20 |
|
| 21 |
def google_search(query):
|
| 22 |
+
"""Fetch search results using web search."""
|
| 23 |
+
results = search(query) # This calls the web tool
|
| 24 |
if results:
|
| 25 |
+
return results[0] # Return first result
|
| 26 |
return "Sorry, I couldn't find an answer on Google."
|
| 27 |
|
| 28 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 30 |
for val in history:
|
| 31 |
+
if val[0]: messages.append({"role": "user", "content": val[0]})
|
| 32 |
+
if val[1]: messages.append({"role": "assistant", "content": val[1]})
|
|
|
|
|
|
|
|
|
|
| 33 |
messages.append({"role": "user", "content": message})
|
| 34 |
|
| 35 |
response = ""
|
| 36 |
+
for message in client.chat_completion(messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
token = message.choices[0].delta.content
|
| 38 |
response += token
|
| 39 |
+
yield response # Stream the response
|
| 40 |
+
|
| 41 |
+
# If the model's response is unreliable, fetch from Google
|
| 42 |
if is_uncertain(message, response):
|
| 43 |
google_response = google_search(message)
|
| 44 |
yield f"🤖 AI: {response}\n\n🌍 Google: {google_response}"
|
|
|
|
| 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(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
],
|
| 54 |
)
|
| 55 |
|