GeminiAi commited on
Commit
66eb634
·
1 Parent(s): 4a04cec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -51
app.py CHANGED
@@ -1,56 +1,73 @@
1
- import streamlit as st
 
2
  import requests
3
- from bs4 import BeautifulSoup
4
 
5
- def fetch_html_content(url):
6
- try:
7
- response = requests.get(url)
8
- response.raise_for_status()
9
- return response.text
10
- except requests.exceptions.RequestException as e:
11
- st.error(f"Error fetching HTML content: {e}")
12
- return None
13
-
14
- def extract_information(html_content, tag, class_name):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  try:
16
- soup = BeautifulSoup(html_content, "html.parser")
17
- elements = soup.find_all(tag, class_=class_name)
18
- return [element.get_text() for element in elements]
19
- except Exception as e:
20
- st.error(f"Error extracting information: {e}")
21
- return []
22
-
23
- def main():
24
- st.title("Website Scraping App")
25
-
26
- # Sidebar for user input
27
- with st.sidebar:
28
- st.header("User Input")
29
- url = st.text_input("Enter Website URL:")
30
- tag = st.text_input("Enter HTML Tag (e.g., 'p'):")
31
- class_name = st.text_input("Enter Class Name (optional):")
32
-
33
- # Main content area
34
- st.write("## Main Content Area")
35
-
36
- if st.button("Fetch HTML Content"):
37
- if url:
38
- html_content = fetch_html_content(url)
39
-
40
- if html_content:
41
- st.success("HTML content fetched successfully!")
42
-
43
- if tag:
44
- extracted_info = extract_information(html_content, tag, class_name)
45
-
46
- st.write("### Extracted Information:")
47
- st.write(extracted_info)
48
- else:
49
- st.warning("Please enter the HTML tag to extract information.")
50
- else:
51
- st.warning("Failed to fetch HTML content. Please check the URL.")
52
  else:
53
- st.warning("Please enter a website URL.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- if __name__ == "__main__":
56
- main()
 
1
+ import gradio as gr
2
+ import os
3
  import requests
 
4
 
5
+ zephyr_7b_beta = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta/"
6
+
7
+ HF_TOKEN = os.getenv("HF_TOKEN")
8
+ HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
9
+
10
+ def build_input_prompt(message, chatbot, system_prompt):
11
+ """
12
+ Constructs the input prompt string from the chatbot interactions and the current message.
13
+ """
14
+ input_prompt = "<|system|>\n" + system_prompt + "</s>\n<|user|>\n"
15
+ for interaction in chatbot:
16
+ input_prompt = input_prompt + str(interaction[0]) + "</s>\n<|assistant|>\n" + str(interaction[1]) + "\n</s>\n<|user|>\n"
17
+
18
+ input_prompt = input_prompt + str(message) + "</s>\n<|assistant|>"
19
+ return input_prompt
20
+
21
+
22
+ def post_request_beta(payload):
23
+ """
24
+ Sends a POST request to the predefined Zephyr-7b-Beta URL and returns the JSON response.
25
+ """
26
+ response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
27
+ response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
28
+ return response.json()
29
+
30
+
31
+ def predict_beta(message, chatbot=[], system_prompt=""):
32
+ input_prompt = build_input_prompt(message, chatbot, system_prompt)
33
+ data = {
34
+ "inputs": input_prompt
35
+ }
36
+
37
  try:
38
+ response_data = post_request_beta(data)
39
+ json_obj = response_data[0]
40
+
41
+ if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
42
+ bot_message = json_obj['generated_text']
43
+ return bot_message
44
+ elif 'error' in json_obj:
45
+ raise gr.Error(json_obj['error'] + ' Please refresh and try again with smaller input prompt')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  else:
47
+ warning_msg = f"Unexpected response: {json_obj}"
48
+ raise gr.Error(warning_msg)
49
+ except requests.HTTPError as e:
50
+ error_msg = f"Request failed with status code {e.response.status_code}"
51
+ raise gr.Error(error_msg)
52
+ except json.JSONDecodeError as e:
53
+ error_msg = f"Failed to decode response as JSON: {str(e)}"
54
+ raise gr.Error(error_msg)
55
+
56
+ def test_preview_chatbot(message, history):
57
+ response = predict_beta(message, history, SYSTEM_PROMPT)
58
+ text_start = response.rfind("<|assistant|>", ) + len("<|assistant|>")
59
+ response = response[text_start:]
60
+ return response
61
+
62
+
63
+ welcome_preview_message = f"""
64
+ Welcome to **{TITLE}**! Say something like:
65
+ "{EXAMPLE_INPUT}"
66
+ """
67
+
68
+ chatbot_preview = gr.Chatbot(layout="panel", value=[(None, welcome_preview_message)])
69
+ textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
70
+
71
+ demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview)
72
 
73
+ demo.launch()