import gradio as gr import requests import importlib from bs4 import BeautifulSoup from huggingface_hub import InferenceClient # Import weather script weather = importlib.import_module("weather") # Hugging Face model client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf") def google_search(query): """Scrape Google search for an answer.""" url = f"https://www.google.com/search?q={query}" headers = {"User-Agent": "Mozilla/5.0"} try: response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, "html.parser") result = soup.find("div", class_="BNeawe iBp4i AP7Wnd") if result: return result.text return "Samahani, siwezi kupata majibu." except Exception: return "Samahani, siwezi kuwasiliana na Google kwa sasa." def respond(message, history, system_message, max_tokens, temperature, top_p): """Chatbot that answers user and fetches real-time info if needed.""" # Handle weather requests if "weather" in message.lower() or "hali ya hewa" in message.lower(): city = message.split()[-1] # Last word as city name return weather.get_weather(city) # Handle time requests if "time" in message.lower() or "saa ngapi" in message.lower(): from datetime import datetime return f"Saa ya sasa ni {datetime.now().strftime('%H:%M:%S')}." # Model response 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 # If model doesn't know, fetch from Google if "I don't know" in response or response.strip() == "": response = google_search(message) return response # Gradio UI 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()