Spaces:
Sleeping
Sleeping
Commit
·
bb5d24f
1
Parent(s):
5ab8b76
test
Browse files
app.py
CHANGED
@@ -20,11 +20,13 @@ from langchain.schema.runnable import RunnablePassthrough
|
|
20 |
from langchain_core.messages import AIMessage, HumanMessage
|
21 |
from langchain_community.llms import HuggingFaceEndpoint
|
22 |
from dotenv import load_dotenv
|
23 |
-
from huggingface_hub import
|
24 |
|
25 |
#zero = torch.Tensor([0]).cuda()
|
26 |
load_dotenv()
|
27 |
api_token = os.getenv("HF_TOKEN")
|
|
|
|
|
28 |
#@spaces.GPU
|
29 |
def read_pdf(file_path):
|
30 |
output = ''
|
@@ -51,15 +53,43 @@ def read_pdf(file_path):
|
|
51 |
|
52 |
# Function to query Hugging Face endpoint
|
53 |
#@spaces.GPU
|
54 |
-
def
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
# Gradio Interface for PDF Processing
|
60 |
def process_file(file, query):
|
61 |
pdf_output = read_pdf(file.name)
|
62 |
-
huggingface_output =
|
|
|
63 |
return pdf_output, huggingface_output
|
64 |
|
65 |
# Create Gradio App
|
|
|
20 |
from langchain_core.messages import AIMessage, HumanMessage
|
21 |
from langchain_community.llms import HuggingFaceEndpoint
|
22 |
from dotenv import load_dotenv
|
23 |
+
from huggingface_hub import InferenceClient
|
24 |
|
25 |
#zero = torch.Tensor([0]).cuda()
|
26 |
load_dotenv()
|
27 |
api_token = os.getenv("HF_TOKEN")
|
28 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
29 |
+
|
30 |
#@spaces.GPU
|
31 |
def read_pdf(file_path):
|
32 |
output = ''
|
|
|
53 |
|
54 |
# Function to query Hugging Face endpoint
|
55 |
#@spaces.GPU
|
56 |
+
def respond(
|
57 |
+
message,
|
58 |
+
history: list[tuple[str, str]],
|
59 |
+
system_message,
|
60 |
+
max_tokens,
|
61 |
+
temperature,
|
62 |
+
top_p,
|
63 |
+
):
|
64 |
+
messages = [{"role": "system", "content": system_message}]
|
65 |
+
|
66 |
+
for val in history:
|
67 |
+
if val[0]:
|
68 |
+
messages.append({"role": "user", "content": val[0]})
|
69 |
+
if val[1]:
|
70 |
+
messages.append({"role": "assistant", "content": val[1]})
|
71 |
+
|
72 |
+
messages.append({"role": "user", "content": message})
|
73 |
+
|
74 |
+
response = ""
|
75 |
+
|
76 |
+
for message in client.chat_completion(
|
77 |
+
messages,
|
78 |
+
max_tokens=max_tokens,
|
79 |
+
stream=True,
|
80 |
+
temperature=temperature,
|
81 |
+
top_p=top_p,
|
82 |
+
):
|
83 |
+
token = message.choices[0].delta.content
|
84 |
+
|
85 |
+
response += token
|
86 |
+
yield response
|
87 |
|
88 |
# Gradio Interface for PDF Processing
|
89 |
def process_file(file, query):
|
90 |
pdf_output = read_pdf(file.name)
|
91 |
+
huggingface_output = respond(query, history=[], system_message="You are a friendly Chatbot.",
|
92 |
+
max_tokens=1024, temperature=0.0, top_p=1.0 )
|
93 |
return pdf_output, huggingface_output
|
94 |
|
95 |
# Create Gradio App
|