suhail0318 commited on
Commit
2e0e1df
·
verified ·
1 Parent(s): 3731340

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -54
app.py CHANGED
@@ -1,64 +1,95 @@
 
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]})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- messages.append({"role": "user", "content": message})
 
 
27
 
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
  ],
 
 
 
60
  )
61
 
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
1
+ import openai
2
  import gradio as gr
3
+ import requests
4
+ from bs4 import BeautifulSoup
5
 
6
+ # Initialize OpenAI with your API key
7
+ openai.api_key = "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"
 
 
8
 
9
+ # Function to fetch and crawl website content
10
+ def fetch_website_content(url):
11
+ try:
12
+ # Send a GET request to the website
13
+ response = requests.get(url)
14
+ if response.status_code != 200:
15
+ return "Error: Could not fetch the webpage. Please check the URL."
16
+
17
+ # Parse the website content with BeautifulSoup
18
+ soup = BeautifulSoup(response.content, 'html.parser')
19
+
20
+ # Extract text content from paragraph tags
21
+ website_text = " ".join([p.text for p in soup.find_all('p')])
22
+ return website_text
23
+
24
+ except Exception as e:
25
+ return f"Error: {str(e)}"
26
 
27
+ # Function to split content into chunks that fit within the token limits
28
+ def split_content_into_chunks(content, max_chunk_size=3000):
29
+ # Split the content into chunks based on token limits
30
+ words = content.split()
31
+ chunks = []
32
+
33
+ while words:
34
+ chunk = words[:max_chunk_size]
35
+ chunks.append(" ".join(chunk))
36
+ words = words[max_chunk_size:]
37
+
38
+ return chunks
39
 
40
+ # Function to query GPT model with website content
41
+ def ask_question(url, question):
42
+ # Fetch website content
43
+ website_text = fetch_website_content(url)
44
+
45
+ if "Error" in website_text:
46
+ return website_text
47
+
48
+ # Split content into manageable chunks based on OpenAI's token limit
49
+ chunks = split_content_into_chunks(website_text)
50
+
51
+ # Initialize a variable to hold the entire response
52
+ full_answer = ""
53
+
54
+ # Query GPT model for each chunk
55
+ for chunk in chunks:
56
+ # Prepare the prompt for GPT
57
+ messages = [
58
+ {"role": "system", "content": "You are a helpful assistant who answers questions based on the following website content."},
59
+ {"role": "user", "content": f"Website content: {chunk}\n\nQuestion: {question}"}
60
+ ]
61
+
62
+ # Use GPT-3.5-turbo model to generate an answer
63
+ try:
64
+ response = openai.ChatCompletion.create(
65
+ model="gpt-3.5-turbo", # Use gpt-4 if you have access to it
66
+ messages=messages,
67
+ max_tokens=3000, # Increase max_tokens to the highest possible value
68
+ temperature=0.5,
69
+ )
70
+ answer = response.choices[0].message['content'].strip()
71
+ full_answer += answer + "\n\n" # Append chunked responses together
72
+
73
+ except Exception as e:
74
+ return f"Error: {str(e)}"
75
+
76
+ return full_answer
77
 
78
+ # Gradio interface for chatbot
79
+ def chatbot(url, question):
80
+ return ask_question(url, question)
81
 
82
+ # Define Gradio interface using new syntax
83
+ iface = gr.Interface(
84
+ fn=chatbot,
85
+ inputs=[
86
+ gr.Textbox(label="Website URL", placeholder="Enter website URL here..."),
87
+ gr.Textbox(label="Your Question", placeholder="Ask a question to understand what is in the website or generate article based on the website information...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  ],
89
+ outputs=gr.Textbox(),
90
+ title="Contentigo - Lite",
91
+ description="Ask questions about the content of any website. Also, generate articles based on the website content."
92
  )
93
 
94
+ # Launch the Gradio interface
95
+ iface.launch()