ashutoshsharma58 commited on
Commit
56cf1b6
·
verified ·
1 Parent(s): 7ed32cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -45
app.py CHANGED
@@ -1,49 +1,24 @@
1
- from flask import Flask, request, jsonify
2
- from transformers import pipeline
3
- import requests
4
- from bs4 import BeautifulSoup
5
 
6
- app = Flask(__name__)
 
 
 
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
- @app.route('/')
15
- def index():
16
- return "Chatbot is running!"
 
 
 
 
 
 
17
 
18
- @app.route('/chat', methods=['POST'])
19
- def chat():
20
- try:
21
- user_input = request.json.get('message')
22
- if user_input.startswith('http://') or user_input.startswith('https://'):
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]})