Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import gradio as gr
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# Web scraping
|
| 8 |
+
def scrape_website(url):
|
| 9 |
+
response = requests.get(url)
|
| 10 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 11 |
+
content = ' '.join([p.text for p in soup.find_all('p')])
|
| 12 |
+
return content
|
| 13 |
+
|
| 14 |
+
# Store data
|
| 15 |
+
stored_data = {}
|
| 16 |
+
def store_data(url, content):
|
| 17 |
+
stored_data[url] = content
|
| 18 |
+
|
| 19 |
+
# Conversational AI with a smaller model
|
| 20 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
|
| 21 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")
|
| 22 |
+
|
| 23 |
+
# Move model to GPU if available
|
| 24 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 25 |
+
model.to(device)
|
| 26 |
+
|
| 27 |
+
def generate_response(input_text):
|
| 28 |
+
input_ids = tokenizer.encode(input_text + tokenizer.eos_token, return_tensors='pt').to(device)
|
| 29 |
+
response_ids = model.generate(input_ids, max_length=50, pad_token_id=tokenizer.eos_token_id)
|
| 30 |
+
response = tokenizer.decode(response_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 31 |
+
return response
|
| 32 |
+
|
| 33 |
+
def chatbot_response(user_input):
|
| 34 |
+
if user_input.startswith('http'):
|
| 35 |
+
url = user_input
|
| 36 |
+
if url in stored_data:
|
| 37 |
+
content = stored_data[url]
|
| 38 |
+
else:
|
| 39 |
+
content = scrape_website(url)
|
| 40 |
+
store_data(url, content)
|
| 41 |
+
return "I've fetched the data from the website. How can I help you with it?"
|
| 42 |
+
else:
|
| 43 |
+
response = generate_response(user_input)
|
| 44 |
+
return response
|
| 45 |
+
|
| 46 |
+
# Interface
|
| 47 |
+
def chat_interface(user_input):
|
| 48 |
+
return chatbot_response(user_input)
|
| 49 |
+
|
| 50 |
+
iface = gr.Interface(fn=chat_interface, inputs="text", outputs="text")
|
| 51 |
+
iface.launch()
|