FuturesonyAi / app.py
Futuresony's picture
Update app.py
18c3fd1 verified
raw
history blame
2.77 kB
import gradio as gr
from huggingface_hub import InferenceClient
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
def is_uncertain(question, response):
"""Check if the model's response is unreliable."""
if len(response.split()) < 4 or response.lower() in question.lower():
return True
uncertain_phrases = ["Kulingana na utafiti", "Inaaminika kuwa", "Ninadhani", "It is believed that", "Some people say"]
return any(phrase.lower() in response.lower() for phrase in uncertain_phrases)
def google_search(query):
"""Fetch search results using Selenium."""
options = webdriver.ChromeOptions()
options.add_argument("--headless") # Run in background
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get(f"https://www.google.com/search?q={query}")
time.sleep(2) # Wait for page to load
try:
# Extract answer from featured snippet if available
snippet = driver.find_element(By.CLASS_NAME, "hgKElc").text
except:
# Extract first search result
try:
snippet = driver.find_element(By.CSS_SELECTOR, "div.BNeawe.s3v9rd.AP7Wnd").text
except:
snippet = "Sorry, I couldn't find an answer on Google."
driver.quit()
return snippet
def respond(message, history, system_message, max_tokens, temperature, top_p):
messages = [{"role": "system", "content": system_message}]
for val in history:
if val[0]: messages.append({"role": "user", "content": val[0]})
if val[1]: messages.append({"role": "assistant", "content": val[1]})
messages.append({"role": "user", "content": message})
response = ""
for message in client.chat_completion(messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p):
token = message.choices[0].delta.content
response += token
yield response # Stream the response
if is_uncertain(message, response):
google_response = google_search(message)
yield f"πŸ€– AI: {response}\n\n🌍 Google: {google_response}"
demo = gr.ChatInterface(
respond,
additional_inputs=[
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
],
)
if __name__ == "__main__":
demo.launch()