Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
from PyPDF2 import PdfReader
|
|
|
|
| 4 |
|
| 5 |
# PDF ํ
์คํธ ๋ฏธ๋ฆฌ ์ฝ์ด์ค๊ธฐ
|
| 6 |
def extract_pdf_text(pdf_paths):
|
|
@@ -13,34 +14,41 @@ def extract_pdf_text(pdf_paths):
|
|
| 13 |
full_text += text + "\n"
|
| 14 |
return full_text.strip()
|
| 15 |
|
| 16 |
-
#
|
| 17 |
pdf_context = extract_pdf_text([
|
| 18 |
"assets/Programming-Fundamentals-1570222270.pdf",
|
| 19 |
"assets/1๋ถํ์ด์ฌ_๊ฐ์์๋ฃ_์ ์ฒด.pdf"
|
| 20 |
])
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
{"role": "user", "content": f"์๋๋ ํ์ด์ฌ ํ๋ก๊ทธ๋๋ฐ API ๋ ํผ๋ฐ์ค์
๋๋ค:\n{pdf_context}\n\n์ง๋ฌธ: {message}"}
|
| 29 |
-
]
|
| 30 |
-
|
| 31 |
for user_msg, bot_msg in history:
|
| 32 |
if user_msg:
|
| 33 |
messages.append({"role": "user", "content": user_msg})
|
| 34 |
if bot_msg:
|
| 35 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
response = ""
|
| 38 |
for chunk in client.chat_completion(
|
| 39 |
-
messages,
|
| 40 |
max_tokens=max_tokens,
|
| 41 |
-
stream=True,
|
| 42 |
temperature=temperature,
|
| 43 |
top_p=top_p,
|
|
|
|
| 44 |
):
|
| 45 |
delta = chunk.choices[0].delta.content
|
| 46 |
if delta:
|
|
@@ -50,13 +58,13 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 50 |
demo = gr.ChatInterface(
|
| 51 |
fn=respond,
|
| 52 |
additional_inputs=[
|
| 53 |
-
gr.Textbox(value="You are a
|
| 54 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 55 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 56 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p
|
| 57 |
],
|
| 58 |
-
title="๐ ํ์ด์ฌ API ๋ ํผ๋ฐ์ค ์ฑ๋ด",
|
| 59 |
-
description="ํ๊ตญ๊ณต๋ ์์
์๋ฃ
|
| 60 |
)
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
from PyPDF2 import PdfReader
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
# PDF ํ
์คํธ ๋ฏธ๋ฆฌ ์ฝ์ด์ค๊ธฐ
|
| 7 |
def extract_pdf_text(pdf_paths):
|
|
|
|
| 14 |
full_text += text + "\n"
|
| 15 |
return full_text.strip()
|
| 16 |
|
| 17 |
+
# ๋ฏธ๋ฆฌ ์ง์ ๋ PDF ๋ฌธ์๋ค
|
| 18 |
pdf_context = extract_pdf_text([
|
| 19 |
"assets/Programming-Fundamentals-1570222270.pdf",
|
| 20 |
"assets/1๋ถํ์ด์ฌ_๊ฐ์์๋ฃ_์ ์ฒด.pdf"
|
| 21 |
])
|
| 22 |
|
| 23 |
+
# Inference Client ์ค์ - ๋ชจ๋ธ ๋ณ๊ฒฝ๋จ
|
| 24 |
+
client = InferenceClient(
|
| 25 |
+
model="mistralai/Mistral-7B-Instruct-v0.1",
|
| 26 |
+
token=os.getenv("HUGGINGFACEHUB_API_TOKEN") # ๋ฐ๋์ ๋ฑ๋ก ํ์
|
| 27 |
+
)
|
| 28 |
|
| 29 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 30 |
+
messages = [{"role": "system", "content": system_message}]
|
| 31 |
+
|
| 32 |
+
# history ๊ธฐ๋ฐ message ๊ตฌ์ฑ
|
|
|
|
|
|
|
|
|
|
| 33 |
for user_msg, bot_msg in history:
|
| 34 |
if user_msg:
|
| 35 |
messages.append({"role": "user", "content": user_msg})
|
| 36 |
if bot_msg:
|
| 37 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 38 |
|
| 39 |
+
# ๋ฌธ์ ๊ธฐ๋ฐ ์ง๋ฌธ ๊ตฌ์ฑ
|
| 40 |
+
messages.append({
|
| 41 |
+
"role": "user",
|
| 42 |
+
"content": f"๋ค์์ ํ์ด์ฌ ํ๋ก๊ทธ๋๋ฐ ๋ฌธ์์
๋๋ค:\n\n{pdf_context}\n\n์ง๋ฌธ: {message}"
|
| 43 |
+
})
|
| 44 |
+
|
| 45 |
response = ""
|
| 46 |
for chunk in client.chat_completion(
|
| 47 |
+
messages=messages,
|
| 48 |
max_tokens=max_tokens,
|
|
|
|
| 49 |
temperature=temperature,
|
| 50 |
top_p=top_p,
|
| 51 |
+
stream=True,
|
| 52 |
):
|
| 53 |
delta = chunk.choices[0].delta.content
|
| 54 |
if delta:
|
|
|
|
| 58 |
demo = gr.ChatInterface(
|
| 59 |
fn=respond,
|
| 60 |
additional_inputs=[
|
| 61 |
+
gr.Textbox(value="You are a helpful assistant answering based on the programming reference.", label="System message"),
|
| 62 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 63 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 64 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
| 65 |
],
|
| 66 |
+
title="๐ ํ์ด์ฌ API ๋ ํผ๋ฐ์ค ์ฑ๋ด (Mistral ๊ธฐ๋ฐ)",
|
| 67 |
+
description="ํ๊ตญ๊ณต๋ ์์
์๋ฃ ๊ธฐ๋ฐ์ผ๋ก ์ง๋ฌธ์ ๋ต๋ณํ๋ ์ฑ๋ด์
๋๋ค."
|
| 68 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|