Spaces:
Sleeping
Sleeping
File size: 2,162 Bytes
4e6a306 82f7e90 9da9b54 82f7e90 99afb39 82f7e90 4e6a306 99afb39 9da9b54 f30dded 99afb39 9da9b54 4e6a306 82f7e90 99afb39 15d6c6c 4e6a306 15d6c6c 4e6a306 99afb39 4e6a306 82f7e90 4e6a306 99afb39 4e6a306 9da9b54 4e6a306 0d05982 4975a36 4e6a306 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import gradio as gr
from huggingface_hub import InferenceClient
from PyPDF2 import PdfReader
import os
# PDF ํ
์คํธ ๋ฏธ๋ฆฌ ์ฝ์ด์ค๊ธฐ
def extract_pdf_text(pdf_paths):
full_text = ""
for path in pdf_paths:
reader = PdfReader(path)
for page in reader.pages:
text = page.extract_text()
if text:
full_text += text + "\n"
return full_text.strip()
# ๋ฏธ๋ฆฌ ์ง์ ๋ PDF ๋ฌธ์๋ค
pdf_context = extract_pdf_text([
"assets/Programming-Fundamentals-1570222270.pdf",
"assets/1๋ถํ์ด์ฌ_๊ฐ์์๋ฃ_์ ์ฒด.pdf"
])
# Hugging Face Inference Client ์ค์ (starcoderbase ์ฌ์ฉ)
client = InferenceClient(
model="mistralai/Mistral-7B-Instruct-v0.1",
token=os.getenv("HUGGINGFACEHUB_API_TOKEN") # ๋ฐ๋์ ๋ฑ๋ก ํ์
)
def respond(message, history, system_message, max_tokens, temperature, top_p):
# ๋ํ ํ์คํ ๋ฆฌ๋ฅผ ํ
์คํธ๋ก ์ฐ๊ฒฐ
history_text = "\n".join(
[f"Q: {user}\nA: {assistant}" for user, assistant in history if user and assistant]
)
# StarCoder๋ messages ๊ตฌ์กฐ๋ฅผ ์ง์ํ์ง ์์ผ๋ฏ๋ก prompt ๊ธฐ๋ฐ
prompt = f"{system_message}\n\n๋ฌธ์ ์์ฝ:\n{pdf_context}\n\n{history_text}\n\nQ: {message}\nA:"
result = client.text_generation(
prompt=prompt,
max_new_tokens=max_tokens,
temperature=temperature,
top_p=top_p
)
return result.strip()
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
demo = gr.ChatInterface(
fn=respond,
additional_inputs=[
gr.Textbox(value="๋น์ ์ ํ๋ก๊ทธ๋๋ฐ ๋ฌธ์๋ฅผ ์ดํดํ๊ณ ์ฝ๋ ์์ ๋ก ์ค๋ช
ํ๋ ์น์ ํ ์กฐ๊ต์
๋๋ค.", label="System message"),
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
],
title="๐ป ํ์ด์ฌ API ๊ธฐ๋ฐ ์ฝ๋ฉ ๋์ฐ๋ฏธ",
description="ํ์ด์ฌ ๊ฐ์์๋ฃ ๊ธฐ๋ฐ์ผ๋ก ์ฝ๋ ์ง๋ฌธ์ ๋ต๋ณํ๋ StarCoder ์ฑ๋ด์
๋๋ค."
)
if __name__ == "__main__":
demo.launch()
|