Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,24 @@
|
|
| 1 |
-
|
| 2 |
-
from
|
| 3 |
-
import requests
|
| 4 |
-
from bs4 import BeautifulSoup
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
try:
|
| 9 |
-
nlp_pipeline = pipeline('text-generation', model='gpt2', framework='pt')
|
| 10 |
-
print("Hugging Face pipeline initialized successfully.")
|
| 11 |
-
except Exception as e:
|
| 12 |
-
print(f"Error initializing Hugging Face pipeline: {e}")
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
response = handle_url(user_input)
|
| 24 |
-
else:
|
| 25 |
-
response = handle_text(user_input)
|
| 26 |
-
return jsonify(response)
|
| 27 |
-
except Exception as e:
|
| 28 |
-
return jsonify({"message": "Error processing request", "details": str(e)})
|
| 29 |
-
|
| 30 |
-
def handle_url(url):
|
| 31 |
-
try:
|
| 32 |
-
response = requests.get(url)
|
| 33 |
-
soup = BeautifulSoup(response.content, 'html.parser')
|
| 34 |
-
title = soup.title.string if soup.title else "No title found"
|
| 35 |
-
paragraphs = [p.get_text() for p in soup.find_all('p')]
|
| 36 |
-
return {"message": f"Website Title: {title}\n\n" + "\n".join(paragraphs[:2])}
|
| 37 |
-
except Exception as e:
|
| 38 |
-
return {"message": "Error scraping the website", "details": str(e)}
|
| 39 |
-
|
| 40 |
-
def handle_text(text):
|
| 41 |
-
try:
|
| 42 |
-
response = nlp_pipeline(text, max_length=50, num_return_sequences=1)
|
| 43 |
-
return {"message": response[0]['generated_text']}
|
| 44 |
-
except Exception as e:
|
| 45 |
-
return {"message": "Error generating text", "details": str(e)}
|
| 46 |
-
|
| 47 |
-
if __name__ == '__main__':
|
| 48 |
-
print("Starting Flask app...")
|
| 49 |
-
app.run(debug=True, port=5001)
|
|
|
|
| 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("HuggingFaceH4/zephyr-7b-beta")
|
| 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]})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|