Spaces:
Sleeping
Sleeping
Commit
·
b98edd0
1
Parent(s):
b07d334
test
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ from PIL import Image
|
|
2 |
import pytesseract
|
3 |
import os
|
4 |
import pymupdf
|
5 |
-
|
6 |
import torch
|
7 |
import gradio as gr
|
8 |
from prepare import prepare
|
@@ -21,7 +21,7 @@ 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")
|
@@ -53,43 +53,57 @@ def read_pdf(file_path):
|
|
53 |
|
54 |
# Function to query Hugging Face endpoint
|
55 |
#@spaces.GPU
|
56 |
-
def respond(
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
):
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
|
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 |
-
|
73 |
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
-
|
77 |
-
|
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 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
92 |
-
max_tokens=1024, temperature=0.0, top_p=0.99 )
|
|
|
93 |
return pdf_output, huggingface_output
|
94 |
|
95 |
# Create Gradio App
|
|
|
2 |
import pytesseract
|
3 |
import os
|
4 |
import pymupdf
|
5 |
+
import spaces
|
6 |
import torch
|
7 |
import gradio as gr
|
8 |
from prepare import prepare
|
|
|
21 |
from langchain_community.llms import HuggingFaceEndpoint
|
22 |
from dotenv import load_dotenv
|
23 |
from huggingface_hub import InferenceClient
|
24 |
+
import huggingface_hub
|
25 |
#zero = torch.Tensor([0]).cuda()
|
26 |
load_dotenv()
|
27 |
api_token = os.getenv("HF_TOKEN")
|
|
|
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 |
+
# return response
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
+
@spaces.GPU
|
89 |
+
def LLM_Inference(text):
|
90 |
+
huggingface_hub.login(token=api_token)
|
91 |
+
device = torch.device('cuda')
|
92 |
+
tokenizer = AutoTokenizer.from_pretrained('google/gemma-2-2b-it')
|
93 |
+
model = AutoModelForCausalLM.from_pretrained('google/gemma-2-2b-it').to(device)
|
94 |
+
inputs = tokenizer(text, return_tensors='pt').to(device)
|
95 |
+
with torch.no_grad():
|
96 |
+
outputs = model.generate(
|
97 |
+
**inputs, max_new_tokens=128, pad_token_id = tokenizer.eos_token_id
|
98 |
+
)
|
99 |
+
return tokenizer.decode(outputs[0])
|
100 |
|
101 |
# Gradio Interface for PDF Processing
|
102 |
def process_file(file, query):
|
103 |
pdf_output = read_pdf(file.name)
|
104 |
+
#huggingface_output = respond(query, history=[], system_message="You are a friendly sChatbot.",
|
105 |
+
#max_tokens=1024, temperature=0.0, top_p=0.99 )
|
106 |
+
huggingface_output = LLM_Inference(query)
|
107 |
return pdf_output, huggingface_output
|
108 |
|
109 |
# Create Gradio App
|