Fix requirements.txt file
Browse files- app_gradio.py +33 -21
app_gradio.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
|
| 5 |
-
from flask import json
|
| 6 |
|
| 7 |
-
# Flask μ±
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
# Gradio μΈν°νμ΄μ€ μμ±
|
| 11 |
with gr.Blocks(title="RAG κ²μ μ±λ΄ with μμ±μΈμ", theme=gr.themes.Soft()) as demo:
|
|
@@ -38,33 +38,45 @@ with gr.Blocks(title="RAG κ²μ μ±λ΄ with μμ±μΈμ", theme=gr.themes.Soft
|
|
| 38 |
def handle_text_chat(query):
|
| 39 |
if not query:
|
| 40 |
return "μ§λ¬Έμ μ
λ ₯νμΈμ.", ""
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# μμ± μ± κΈ°λ₯
|
| 48 |
def handle_voice_chat(audio_file):
|
| 49 |
if not audio_file:
|
| 50 |
return "μμ±μ μ
λ‘λνμΈμ.", "", ""
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# λ¬Έμ μ
λ‘λ κΈ°λ₯
|
| 59 |
def handle_doc_upload(doc_file):
|
| 60 |
if not doc_file:
|
| 61 |
return "λ¬Έμλ₯Ό μ
λ‘λνμΈμ."
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# μ΄λ²€νΈ νΈλ€λ¬ μ°κ²°
|
| 70 |
text_button.click(
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
+
import json
|
|
|
|
| 5 |
|
| 6 |
+
# Flask μ±μ λ μ΄μ ν
μ€νΈ ν΄λΌμ΄μΈνΈλ‘ μ¬μ©νμ§ μμ
|
| 7 |
+
# λμ , localhost:7860μμ μ€ν μ€μΈ Flask μλν¬μΈνΈμ HTTP μμ²μ 보λ
|
| 8 |
+
BASE_URL = "http://localhost:7860"
|
| 9 |
|
| 10 |
# Gradio μΈν°νμ΄μ€ μμ±
|
| 11 |
with gr.Blocks(title="RAG κ²μ μ±λ΄ with μμ±μΈμ", theme=gr.themes.Soft()) as demo:
|
|
|
|
| 38 |
def handle_text_chat(query):
|
| 39 |
if not query:
|
| 40 |
return "μ§λ¬Έμ μ
λ ₯νμΈμ.", ""
|
| 41 |
+
try:
|
| 42 |
+
response = requests.post(f"{BASE_URL}/api/chat", json={"query": query})
|
| 43 |
+
response.raise_for_status()
|
| 44 |
+
data = response.json()
|
| 45 |
+
if "error" in data:
|
| 46 |
+
return data["error"], ""
|
| 47 |
+
return data["answer"], json.dumps(data["sources"], indent=2)
|
| 48 |
+
except requests.RequestException as e:
|
| 49 |
+
return f"μμ² μ€ μ€λ₯ λ°μ: {str(e)}", ""
|
| 50 |
|
| 51 |
# μμ± μ± κΈ°λ₯
|
| 52 |
def handle_voice_chat(audio_file):
|
| 53 |
if not audio_file:
|
| 54 |
return "μμ±μ μ
λ‘λνμΈμ.", "", ""
|
| 55 |
+
try:
|
| 56 |
+
with open(audio_file, "rb") as f:
|
| 57 |
+
response = requests.post(f"{BASE_URL}/api/voice", files={"audio": f})
|
| 58 |
+
response.raise_for_status()
|
| 59 |
+
data = response.json()
|
| 60 |
+
if "error" in data:
|
| 61 |
+
return "", data["error"], ""
|
| 62 |
+
return data["transcription"], data["answer"], json.dumps(data["sources"], indent=2)
|
| 63 |
+
except requests.RequestException as e:
|
| 64 |
+
return "", f"μμ² μ€ μ€λ₯ λ°μ: {str(e)}", ""
|
| 65 |
|
| 66 |
# λ¬Έμ μ
λ‘λ κΈ°λ₯
|
| 67 |
def handle_doc_upload(doc_file):
|
| 68 |
if not doc_file:
|
| 69 |
return "λ¬Έμλ₯Ό μ
λ‘λνμΈμ."
|
| 70 |
+
try:
|
| 71 |
+
with open(doc_file, "rb") as f:
|
| 72 |
+
response = requests.post(f"{BASE_URL}/api/upload", files={"document": f})
|
| 73 |
+
response.raise_for_status()
|
| 74 |
+
data = response.json()
|
| 75 |
+
if "error" in data:
|
| 76 |
+
return data["error"]
|
| 77 |
+
return data["message"]
|
| 78 |
+
except requests.RequestException as e:
|
| 79 |
+
return f"μμ² μ€ μ€λ₯ λ°μ: {str(e)}"
|
| 80 |
|
| 81 |
# μ΄λ²€νΈ νΈλ€λ¬ μ°κ²°
|
| 82 |
text_button.click(
|