Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,54 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
os.system('pip install transformers')
|
| 3 |
os.system('pip install datasets')
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
from transformers import pipeline
|
| 8 |
from datasets import load_dataset
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# Use a pipeline as a high-level helper
|
| 13 |
-
pipe = pipeline("fill-mask", model="nlpaueb/legal-bert-base-uncased")
|
| 14 |
-
|
| 15 |
-
"""
|
| 16 |
-
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
|
| 17 |
-
"""
|
| 18 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 19 |
|
| 20 |
-
|
| 21 |
-
message,
|
| 22 |
-
history: list[tuple[str, str]],
|
| 23 |
-
system_message,
|
| 24 |
-
max_tokens,
|
| 25 |
-
temperature,
|
| 26 |
-
top_p,
|
| 27 |
-
):
|
| 28 |
-
messages = [{"role": "system", "content": system_message}]
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
|
|
|
|
|
|
| 38 |
response = ""
|
| 39 |
-
|
| 40 |
for message in client.chat_completion(
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
stream=True,
|
| 44 |
-
temperature=
|
| 45 |
-
top_p=
|
| 46 |
):
|
| 47 |
token = message.choices[0].delta.content
|
| 48 |
-
|
| 49 |
-
response += token
|
| 50 |
-
yield response
|
| 51 |
-
token = message.choices[0].delta.content
|
| 52 |
if token is not None:
|
| 53 |
response += token
|
| 54 |
return response
|
|
|
|
| 1 |
import os
|
| 2 |
+
os.system('pip install torch') # or 'pip install tensorflow'
|
| 3 |
os.system('pip install transformers')
|
| 4 |
os.system('pip install datasets')
|
| 5 |
+
os.system('pip install gradio')
|
| 6 |
+
os.system('pip install minijinja')
|
| 7 |
+
os.system('pip install PyMuPDF')
|
| 8 |
|
| 9 |
import gradio as gr
|
| 10 |
from huggingface_hub import InferenceClient
|
| 11 |
from transformers import pipeline
|
| 12 |
from datasets import load_dataset
|
| 13 |
+
import fitz # PyMuPDF
|
| 14 |
|
| 15 |
+
client = InferenceClient()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
dataset = load_dataset("ibunescu/qa_legal_dataset_train")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
def score_argument_from_outcome(outcome, argument):
|
| 20 |
+
prosecutor_score = 0
|
| 21 |
+
if "Prosecutor" in outcome:
|
| 22 |
+
prosecutor_score = outcome.count("Prosecutor") * 2
|
| 23 |
+
if "won" in outcome and "Prosecutor" in outcome:
|
| 24 |
+
prosecutor_score += 10
|
| 25 |
+
return prosecutor_score
|
| 26 |
+
|
| 27 |
+
def chat_between_bots(system_message1, system_message2, max_tokens, temperature, top_p, history1, history2, shared_history, message):
|
| 28 |
+
response1, history1 = list(respond(message, history1, system_message1, max_tokens, temperature, top_p))[-1]
|
| 29 |
+
response2, history2 = list(respond(message, history2, system_message2, max_tokens, temperature, top_p))[-1]
|
| 30 |
+
|
| 31 |
+
return response1, response2, history1, history2, shared_history
|
| 32 |
|
| 33 |
+
def extract_text_from_pdf(pdf_file):
|
| 34 |
+
text = ""
|
| 35 |
+
doc = fitz.open(pdf_file)
|
| 36 |
+
for page in doc:
|
| 37 |
+
text += page.get_text()
|
| 38 |
+
return text
|
| 39 |
|
| 40 |
+
def ask_about_pdf(pdf_text, question):
|
| 41 |
+
prompt = f"PDF Content: {pdf_text}\n\nQuestion: {question}\n\nAnswer:"
|
| 42 |
response = ""
|
|
|
|
| 43 |
for message in client.chat_completion(
|
| 44 |
+
[{"role": "system", "content": "You are a legal expert answering questions based on the PDF content provided."},
|
| 45 |
+
{"role": "user", "content": prompt}],
|
| 46 |
+
max_tokens=512,
|
| 47 |
stream=True,
|
| 48 |
+
temperature=0.6,
|
| 49 |
+
top_p=0.95,
|
| 50 |
):
|
| 51 |
token = message.choices[0].delta.content
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
if token is not None:
|
| 53 |
response += token
|
| 54 |
return response
|