Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import pypandoc
|
4 |
+
import os
|
5 |
+
import tempfile
|
6 |
+
|
7 |
+
# ----------------------
|
8 |
+
# 1. Load the chat model
|
9 |
+
# ----------------------
|
10 |
+
# The Tencent Hunyuan model is large; this will download weights the first time
|
11 |
+
# and fall back to CPU if a GPU isn't available.
|
12 |
+
chat_pipe = pipeline(
|
13 |
+
"text-generation",
|
14 |
+
model="tencent/Hunyuan-A13B-Instruct",
|
15 |
+
trust_remote_code=True,
|
16 |
+
device_map="auto", # uses GPU if possible
|
17 |
+
max_new_tokens=512,
|
18 |
+
)
|
19 |
+
|
20 |
+
# ----------------------
|
21 |
+
# 2. Chat helper
|
22 |
+
# ----------------------
|
23 |
+
|
24 |
+
def chat_ai(history, user_message):
|
25 |
+
"""LLM chat wrapper: takes the chat history + new user msg, returns assistant reply."""
|
26 |
+
|
27 |
+
# Re‑create a messages list compatible with the model’s chat format
|
28 |
+
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
29 |
+
for user, bot in history:
|
30 |
+
messages.append({"role": "user", "content": user})
|
31 |
+
messages.append({"role": "assistant", "content": bot})
|
32 |
+
messages.append({"role": "user", "content": user_message})
|
33 |
+
|
34 |
+
# Generate
|
35 |
+
completion = chat_pipe(messages)[0]["generated_text"]
|
36 |
+
|
37 |
+
# Some chat models return the entire conversation; grab only the last assistant line
|
38 |
+
assistant_reply = completion.split("\n")[-1].strip()
|
39 |
+
return assistant_reply
|
40 |
+
|
41 |
+
# ----------------------
|
42 |
+
# 3. Document converter
|
43 |
+
# ----------------------
|
44 |
+
# Powered by Pandoc via the pypandoc wrapper. Make sure pandoc is available in the
|
45 |
+
# environment (apt-get install pandoc on Linux, or add it to requirements.txt).
|
46 |
+
|
47 |
+
SUPPORTED_TARGETS = [
|
48 |
+
"pdf", "docx", "html", "md", "txt", "rtf", "odt", "epub"
|
49 |
+
]
|
50 |
+
|
51 |
+
|
52 |
+
def convert_document(file_obj, target_format):
|
53 |
+
"""Convert the uploaded file to the selected target format and return the path."""
|
54 |
+
if file_obj is None:
|
55 |
+
raise gr.Error("Please upload a file.")
|
56 |
+
|
57 |
+
if target_format not in SUPPORTED_TARGETS:
|
58 |
+
raise gr.Error(f"Unsupported target format: {target_format}")
|
59 |
+
|
60 |
+
filename = os.path.basename(file_obj.name)
|
61 |
+
src_ext = os.path.splitext(filename)[1].lstrip(".")
|
62 |
+
|
63 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
64 |
+
# Save the uploaded file to a temp path that Pandoc can read
|
65 |
+
src_path = os.path.join(tmpdir, f"input.{src_ext}")
|
66 |
+
with open(src_path, "wb") as f:
|
67 |
+
f.write(file_obj.read())
|
68 |
+
|
69 |
+
# Build output path inside tmp dir
|
70 |
+
output_path = os.path.join(tmpdir, f"converted.{target_format}")
|
71 |
+
|
72 |
+
# Run Pandoc
|
73 |
+
try:
|
74 |
+
pypandoc.convert_file(src_path, to=target_format, outputfile=output_path)
|
75 |
+
except RuntimeError as e:
|
76 |
+
raise gr.Error(f"Conversion failed: {e}")
|
77 |
+
|
78 |
+
# Return as a downloadable file for Gradio
|
79 |
+
return output_path
|
80 |
+
|
81 |
+
# ----------------------
|
82 |
+
# 4. Gradio UI
|
83 |
+
# ----------------------
|
84 |
+
|
85 |
+
with gr.Blocks(title="LLM Chat + Universal Document Converter") as demo:
|
86 |
+
gr.Markdown(
|
87 |
+
"# 🌐 LLM Chat + 📄 Document Converter\n"
|
88 |
+
"Chat with **Hunyuan-A13B** (Tencent) or convert documents between multiple formats using **Pandoc**."
|
89 |
+
)
|
90 |
+
|
91 |
+
with gr.Tabs():
|
92 |
+
# ----- Tab 1: Chat -----
|
93 |
+
with gr.TabItem("Chat"):
|
94 |
+
chatbot = gr.Chatbot()
|
95 |
+
user_msg = gr.Textbox(placeholder="Ask me anything...", label="Your message")
|
96 |
+
send_btn = gr.Button("Send")
|
97 |
+
clear_btn = gr.Button("Clear")
|
98 |
+
|
99 |
+
def _send(history, msg):
|
100 |
+
history = history or []
|
101 |
+
reply = chat_ai(history, msg)
|
102 |
+
history.append((msg, reply))
|
103 |
+
return history, ""
|
104 |
+
|
105 |
+
send_btn.click(_send, inputs=[chatbot, user_msg], outputs=[chatbot, user_msg])
|
106 |
+
clear_btn.click(lambda: [], None, chatbot)
|
107 |
+
|
108 |
+
# ----- Tab 2: Document Converter -----
|
109 |
+
with gr.TabItem("Convert"):
|
110 |
+
file_input = gr.File(label="Upload document")
|
111 |
+
target = gr.Dropdown(SUPPORTED_TARGETS, label="Convert to", value="pdf")
|
112 |
+
convert_btn = gr.Button("Convert")
|
113 |
+
result_file = gr.File(label="Download converted file")
|
114 |
+
|
115 |
+
convert_btn.click(convert_document, inputs=[file_input, target], outputs=result_file)
|
116 |
+
|
117 |
+
demo.launch()
|