Spaces:
Runtime error
Runtime error
Download app.py from abhijeetsarkargis/DeepConvoAI-Web: direct link, hf CLI and curl.
- Browser
- Download file 8.9 kB
-
https://huggingface.co/spaces/abhijeetsarkargis/DeepConvoAI-Web/resolve/main/app.py
- Command line
-
hf download hf://spaces/abhijeetsarkargis/DeepConvoAI-Web/app.py
-
curl -L -o app.py https://huggingface.co/spaces/abhijeetsarkargis/DeepConvoAI-Web/resolve/main/app.py
8.9 kB
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| import json | |
| from bs4 import BeautifulSoup | |
| import requests | |
| # Futuristic, modern custom CSS for DeepConvoAI interface with a purple theme | |
| css = ''' | |
| @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@500;700&family=Roboto:wght@400;500&display=swap'); | |
| body { | |
| margin: 0; | |
| padding: 0; | |
| font-family: 'Roboto', sans-serif; | |
| background: #0d0d0d; | |
| color: #e0e0e0; | |
| } | |
| .gradio-container { | |
| max-width: 1000px !important; | |
| margin: 2rem auto; | |
| background: rgba(15, 15, 15, 0.95); | |
| border-radius: 15px; | |
| padding: 2rem; | |
| box-shadow: 0 15px 30px rgba(0,0,0,0.7); | |
| position: relative; | |
| overflow: hidden; | |
| } | |
| .gradio-container::before { | |
| content: ""; | |
| position: absolute; | |
| top: -50%; | |
| left: -50%; | |
| width: 200%; | |
| height: 200%; | |
| background: linear-gradient(45deg, #8A2BE2, #4B0082, #8A2BE2); | |
| animation: gradientShift 6s linear infinite; | |
| opacity: 0.1; | |
| } | |
| @keyframes gradientShift { | |
| 0% { transform: translate(0, 0) rotate(0deg); } | |
| 50% { transform: translate(20%, 20%) rotate(180deg); } | |
| 100% { transform: translate(0, 0) rotate(360deg); } | |
| } | |
| h1 { | |
| text-align: center; | |
| font-family: 'Orbitron', sans-serif; | |
| font-size: 2.5rem; | |
| font-weight: 700; | |
| color: #8A2BE2; | |
| margin-bottom: 0.5rem; | |
| } | |
| p.description { | |
| text-align: center; | |
| font-size: 1.1rem; | |
| margin-bottom: 1rem; | |
| color: #a0a0a0; | |
| } | |
| footer { | |
| display: none; | |
| } | |
| /* Textbox styling */ | |
| textarea { | |
| border: 2px solid #6A0DAD; | |
| background: #1a1a1a; | |
| border-radius: 10px; | |
| padding: 12px; | |
| font-size: 1rem; | |
| color: #e0e0e0; | |
| } | |
| ''' | |
| # Function to extract text from a webpage | |
| def get_text_from_html(html_content): | |
| soup = BeautifulSoup(html_content, 'html.parser') | |
| for tag in soup(["script", "style", "header", "footer"]): | |
| tag.extract() | |
| return soup.get_text(strip=True) | |
| # Function to perform a web search | |
| def perform_search(query): | |
| search_term = query | |
| all_results = [] | |
| max_chars_per_page = 8000 | |
| with requests.Session() as session: | |
| response = session.get( | |
| url="https://www.google.com/search", | |
| headers={ | |
| "User-Agent": ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) " | |
| "AppleWebKit/537.36 (KHTML, like Gecko) " | |
| "Chrome/111.0.0.0 Safari/537.36 Edg/111.0.0.0") | |
| }, | |
| params={"q": search_term, "num": 3, "udm": 14}, | |
| timeout=5, | |
| verify=False, | |
| ) | |
| response.raise_for_status() | |
| soup = BeautifulSoup(response.text, "html.parser") | |
| result_block = soup.find_all("div", attrs={"class": "g"}) | |
| for result in result_block: | |
| link = result.find("a", href=True)["href"] | |
| try: | |
| webpage_response = session.get( | |
| link, | |
| headers={ | |
| "User-Agent": ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) " | |
| "AppleWebKit/537.36 (KHTML, like Gecko) " | |
| "Chrome/111.0.0.0 Safari/537.0.0.0") | |
| }, | |
| timeout=5, | |
| verify=False | |
| ) | |
| webpage_response.raise_for_status() | |
| visible_text = get_text_from_html(webpage_response.text) | |
| if len(visible_text) > max_chars_per_page: | |
| visible_text = visible_text[:max_chars_per_page] | |
| all_results.append({"link": link, "text": visible_text}) | |
| except requests.exceptions.RequestException: | |
| all_results.append({"link": link, "text": None}) | |
| return all_results | |
| # Initialize inference clients | |
| client_gemma = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3") | |
| client_mixtral = InferenceClient("NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO") | |
| client_llama = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct") | |
| # Function to handle responses | |
| def chat_response(message, history): | |
| func_calls = [] | |
| functions_metadata = [ | |
| { | |
| "type": "function", | |
| "function": { | |
| "name": "web_search", | |
| "description": "Search query on Google", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "query": { | |
| "type": "string", | |
| "description": "Web search query" | |
| } | |
| }, | |
| "required": ["query"] | |
| } | |
| } | |
| } | |
| ] | |
| for msg in history: | |
| func_calls.append({"role": "user", "content": f"{msg[0]}"}) | |
| func_calls.append({"role": "assistant", "content": f"{msg[1]}"}) | |
| func_calls.append({ | |
| "role": "user", | |
| "content": (f'[SYSTEM] You are a helpful assistant named DeepConvoAI with integrated search capabilities. ' | |
| f'You have access to the following functions:\n{functions_metadata}\n\n' | |
| f'To use these functions respond with:\n' | |
| f'<functioncall> {{ "name": "function_name", "arguments": {{ "arg_1": "value_1", ... }} }} </functioncall>\n' | |
| f'[USER] {message}') | |
| }) | |
| response = client_gemma.chat_completion(func_calls, max_tokens=200) | |
| response = str(response) | |
| try: | |
| response = response[response.find("{"):response.rindex("}")+1] | |
| except Exception: | |
| response = response[response.find("{"):response.rfind("}")+1] | |
| response = response.replace("\\n", "").replace("\\'", "'").replace('\\"', '"').replace('\\', '') | |
| print(f"\n{response}") | |
| try: | |
| json_data = json.loads(response) | |
| if json_data["name"] == "web_search": | |
| query = json_data["arguments"]["query"] | |
| gr.Info("Initiating web search...") | |
| web_results = perform_search(query) | |
| gr.Info("Extracting relevant information...") | |
| web_summary = ' '.join([ | |
| f"Link: {res['link']}\nText: {res['text']}\n\n" | |
| for res in web_results if res['text'] | |
| ]) | |
| messages = "system\nDeepConvoAI uses advanced search capabilities for chat responses." | |
| for msg in history: | |
| messages += f"\nuser\n{msg[0]}" | |
| messages += f"\nassistant\n{msg[1]}" | |
| messages += f"\nuser\n{message}\nweb_result\n{web_summary}\nassistant\n" | |
| stream = client_mixtral.text_generation( | |
| messages, max_new_tokens=2000, do_sample=True, stream=True, | |
| details=True, return_full_text=False | |
| ) | |
| output = "" | |
| for response in stream: | |
| if response.token.text != "": | |
| output += response.token.text | |
| yield output | |
| else: | |
| messages = "system\nDeepConvoAI uses advanced search capabilities for chat responses." | |
| for msg in history: | |
| messages += f"\nuser\n{msg[0]}" | |
| messages += f"\nassistant\n{msg[1]}" | |
| messages += f"\nuser\n{message}\nassistant\n" | |
| stream = client_llama.text_generation( | |
| messages, max_new_tokens=2000, do_sample=True, stream=True, | |
| details=True, return_full_text=False | |
| ) | |
| output = "" | |
| for response in stream: | |
| if response.token.text != "": | |
| output += response.token.text | |
| yield output | |
| except Exception: | |
| messages = "system\nDeepConvoAI uses advanced search capabilities for chat responses." | |
| for msg in history: | |
| messages += f"\nuser\n{msg[0]}" | |
| messages += f"\nassistant\n{msg[1]}" | |
| messages += f"\nuser\n{message}\nassistant\n" | |
| stream = client_llama.text_generation( | |
| messages, max_new_tokens=2000, do_sample=True, stream=True, | |
| details=True, return_full_text=False | |
| ) | |
| output = "" | |
| for response in stream: | |
| if response.token.text != "": | |
| output += response.token.text | |
| yield output | |
| # Create Gradio interface with futuristic aesthetics and improved text readability | |
| demo = gr.ChatInterface( | |
| fn=chat_response, | |
| chatbot=gr.Chatbot(), | |
| description=""" | |
| <h1>DeepConvoAI by Abhijeet Sarkar</h1> | |
| <p class="description"> | |
| Welcome to DeepConvoAI – your intelligent assistant powered by advanced AI and integrated web search. | |
| Explore the future of conversation and discovery. | |
| </p> | |
| """, | |
| textbox=gr.Textbox(placeholder="Ask anything here...", lines=2), | |
| multimodal=False, | |
| concurrency_limit=200, | |
| css=css, | |
| theme="allenai/gradio-theme", | |
| ) | |
| demo.launch(share=True) | |