Spaces:
Sleeping
Sleeping
Commit
·
6d38d15
1
Parent(s):
4870b13
Refactored to model, view, data
Browse files
app.py
CHANGED
@@ -1,67 +1,7 @@
|
|
1 |
-
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
-
|
4 |
-
from langchain_community.document_loaders import UnstructuredURLLoader
|
5 |
-
|
6 |
-
import os
|
7 |
-
|
8 |
-
"""
|
9 |
-
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
|
10 |
-
"""
|
11 |
-
client = InferenceClient("meta-llama/Llama-3.2-1B-Instruct", token=os.getenv("HUGGINGFACEHUB_API_TOKEN"))
|
12 |
-
|
13 |
-
def respond(
|
14 |
-
message,
|
15 |
-
history: list[tuple[str, str]],
|
16 |
-
url,
|
17 |
-
max_tokens,
|
18 |
-
temperature,
|
19 |
-
top_p,
|
20 |
-
):
|
21 |
-
urls = [
|
22 |
-
url,
|
23 |
-
]
|
24 |
-
loader = UnstructuredURLLoader(urls=urls)
|
25 |
-
data = loader.load()
|
26 |
-
|
27 |
-
context = data[0].page_content # will come from 'url'
|
28 |
-
|
29 |
-
prompt = f"""
|
30 |
-
Use the following piece of context to answer the question asked.
|
31 |
-
Please try to provide the answer only based on the context
|
32 |
-
{context}
|
33 |
-
Question:{message}
|
34 |
-
Helpful Answers:
|
35 |
-
"""
|
36 |
-
|
37 |
-
messages = [{"role": "system", "content": url}]
|
38 |
-
|
39 |
-
for val in history:
|
40 |
-
if val[0]:
|
41 |
-
messages.append({"role": "user", "content": val[0]})
|
42 |
-
if val[1]:
|
43 |
-
messages.append({"role": "assistant", "content": val[1]})
|
44 |
-
|
45 |
-
messages.append({"role": "user", "content": prompt})
|
46 |
-
|
47 |
-
response = ""
|
48 |
-
|
49 |
-
for message in client.chat_completion(
|
50 |
-
messages,
|
51 |
-
max_tokens=max_tokens,
|
52 |
-
stream=True,
|
53 |
-
temperature=temperature,
|
54 |
-
top_p=top_p,
|
55 |
-
):
|
56 |
-
token = message.choices[0].delta.content
|
57 |
-
|
58 |
-
response += token
|
59 |
-
yield response
|
60 |
|
|
|
61 |
|
62 |
-
"""
|
63 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
64 |
-
"""
|
65 |
with gr.Blocks() as demo:
|
66 |
with gr.Row(equal_height=True):
|
67 |
with gr.Column(min_width=200, scale=0):
|
@@ -78,7 +18,7 @@ with gr.Blocks() as demo:
|
|
78 |
with gr.Column():
|
79 |
url = gr.Textbox(value="https://www.gradio.app/docs/gradio/chatinterface", label="Docs URL", render=True)
|
80 |
chat = gr.ChatInterface(
|
81 |
-
respond,
|
82 |
additional_inputs=[
|
83 |
url,
|
84 |
max_tokens,
|
|
|
1 |
+
from model import model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
import gradio as gr
|
4 |
|
|
|
|
|
|
|
5 |
with gr.Blocks() as demo:
|
6 |
with gr.Row(equal_height=True):
|
7 |
with gr.Column(min_width=200, scale=0):
|
|
|
18 |
with gr.Column():
|
19 |
url = gr.Textbox(value="https://www.gradio.app/docs/gradio/chatinterface", label="Docs URL", render=True)
|
20 |
chat = gr.ChatInterface(
|
21 |
+
model.respond,
|
22 |
additional_inputs=[
|
23 |
url,
|
24 |
max_tokens,
|
data.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_community.document_loaders import UnstructuredURLLoader
|
2 |
+
|
3 |
+
class Data:
|
4 |
+
def __init__(self, url):
|
5 |
+
self.url = url
|
6 |
+
|
7 |
+
def get_context(self):
|
8 |
+
urls = [
|
9 |
+
self.url,
|
10 |
+
]
|
11 |
+
loader = UnstructuredURLLoader(urls=urls)
|
12 |
+
data = loader.load()
|
13 |
+
|
14 |
+
context = data[0].page_content # will come from 'url'
|
15 |
+
|
16 |
+
return context
|
17 |
+
|
18 |
+
def build_prompt(self, question):
|
19 |
+
prompt = f"""
|
20 |
+
Use the following piece of context to answer the question asked.
|
21 |
+
Please try to provide the answer only based on the context
|
22 |
+
{self.get_context()}
|
23 |
+
Question:{question}
|
24 |
+
Helpful Answers:
|
25 |
+
"""
|
26 |
+
return prompt
|
model.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
|
5 |
+
from data import Data
|
6 |
+
|
7 |
+
class Model:
|
8 |
+
def __init__(self, model_id="meta-llama/Llama-3.2-1B-Instruct"):
|
9 |
+
self.client = InferenceClient(model_id, token=os.getenv("HUGGINGFACEHUB_API_TOKEN"))
|
10 |
+
|
11 |
+
def respond(
|
12 |
+
self,
|
13 |
+
message,
|
14 |
+
history: list[tuple[str, str]],
|
15 |
+
url,
|
16 |
+
max_tokens,
|
17 |
+
temperature,
|
18 |
+
top_p,
|
19 |
+
):
|
20 |
+
|
21 |
+
data = Data(url)
|
22 |
+
|
23 |
+
messages = [{"role": "system", "content": url}]
|
24 |
+
|
25 |
+
for val in history:
|
26 |
+
if val[0]:
|
27 |
+
messages.append({"role": "user", "content": val[0]})
|
28 |
+
if val[1]:
|
29 |
+
messages.append({"role": "assistant", "content": val[1]})
|
30 |
+
|
31 |
+
messages.append({"role": "user", "content": data.build_prompt(message)})
|
32 |
+
|
33 |
+
response = ""
|
34 |
+
|
35 |
+
for message in self.client.chat_completion(
|
36 |
+
messages,
|
37 |
+
max_tokens=max_tokens,
|
38 |
+
stream=True,
|
39 |
+
temperature=temperature,
|
40 |
+
top_p=top_p,
|
41 |
+
):
|
42 |
+
token = message.choices[0].delta.content
|
43 |
+
|
44 |
+
response += token
|
45 |
+
yield response
|
46 |
+
|
47 |
+
|
48 |
+
model = Model()
|