Spaces:
Runtime error
Runtime error
Update app.py4
Browse files
app.py4
CHANGED
|
@@ -1,68 +1,64 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
from selenium import webdriver
|
| 4 |
-
from selenium.webdriver.common.by import By
|
| 5 |
-
from selenium.webdriver.chrome.service import Service
|
| 6 |
-
from webdriver_manager.chrome import ChromeDriverManager
|
| 7 |
-
import time
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
|
| 10 |
|
| 11 |
-
def is_uncertain(question, response):
|
| 12 |
-
"""Check if the model's response is unreliable."""
|
| 13 |
-
if len(response.split()) < 4 or response.lower() in question.lower():
|
| 14 |
-
return True
|
| 15 |
-
uncertain_phrases = ["Kulingana na utafiti", "Inaaminika kuwa", "Ninadhani", "It is believed that", "Some people say"]
|
| 16 |
-
return any(phrase.lower() in response.lower() for phrase in uncertain_phrases)
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
try:
|
| 28 |
-
# Extract answer from featured snippet if available
|
| 29 |
-
snippet = driver.find_element(By.CLASS_NAME, "hgKElc").text
|
| 30 |
-
except:
|
| 31 |
-
# Extract first search result
|
| 32 |
-
try:
|
| 33 |
-
snippet = driver.find_element(By.CSS_SELECTOR, "div.BNeawe.s3v9rd.AP7Wnd").text
|
| 34 |
-
except:
|
| 35 |
-
snippet = "Sorry, I couldn't find an answer on Google."
|
| 36 |
-
|
| 37 |
-
driver.quit()
|
| 38 |
-
return snippet
|
| 39 |
-
|
| 40 |
-
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 41 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 42 |
for val in history:
|
| 43 |
-
if val[0]:
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
messages.append({"role": "user", "content": message})
|
| 46 |
|
| 47 |
response = ""
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
token = message.choices[0].delta.content
|
|
|
|
| 50 |
response += token
|
| 51 |
-
yield response
|
| 52 |
|
| 53 |
-
if is_uncertain(message, response):
|
| 54 |
-
google_response = google_search(message)
|
| 55 |
-
yield f"🤖 AI: {response}\n\n🌍 Google: {google_response}"
|
| 56 |
|
|
|
|
|
|
|
|
|
|
| 57 |
demo = gr.ChatInterface(
|
| 58 |
respond,
|
| 59 |
additional_inputs=[
|
| 60 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 61 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 62 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 63 |
-
gr.Slider(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
],
|
| 65 |
)
|
| 66 |
|
|
|
|
| 67 |
if __name__ == "__main__":
|
| 68 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
"""
|
| 5 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
+
"""
|
| 7 |
client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def respond(
|
| 11 |
+
message,
|
| 12 |
+
history: list[tuple[str, str]],
|
| 13 |
+
system_message,
|
| 14 |
+
max_tokens,
|
| 15 |
+
temperature,
|
| 16 |
+
top_p,
|
| 17 |
+
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
+
|
| 20 |
for val in history:
|
| 21 |
+
if val[0]:
|
| 22 |
+
messages.append({"role": "user", "content": val[0]})
|
| 23 |
+
if val[1]:
|
| 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(
|
| 31 |
+
messages,
|
| 32 |
+
max_tokens=max_tokens,
|
| 33 |
+
stream=True,
|
| 34 |
+
temperature=temperature,
|
| 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 |
demo.launch()
|