gfathertech commited on
Commit
f1806a7
Β·
verified Β·
1 Parent(s): b0d61e0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import httpx
3
+ import gradio as gr
4
+
5
+ # Defaulting to your Hugging Face space backend endpoint
6
+ BACKEND_URL = os.getenv("BACKEND_URL", "https://gfathertech-mymodel.hf.space").rstrip("/")
7
+ API_KEY = os.getenv("API_KEY", "")
8
+
9
+ def get_headers():
10
+ headers = {}
11
+ if API_KEY:
12
+ headers["X-API-Key"] = API_KEY
13
+ headers["Authorization"] = f"Bearer {API_KEY}"
14
+ return headers
15
+
16
+ # 1. File Processing Handler
17
+ def process_file_api(file_obj, language, target_fmt):
18
+ if file_obj is None:
19
+ return {"error": "Please select a file to upload."}
20
+
21
+ url = f"{BACKEND_URL}/convert/file"
22
+ headers = get_headers()
23
+
24
+ try:
25
+ with open(file_obj.name, "rb") as f:
26
+ files = {"file": (os.path.basename(file_obj.name), f, "application/octet-stream")}
27
+ data = {"language": language, "to": target_fmt}
28
+
29
+ with httpx.Client(timeout=60.0) as client:
30
+ response = client.post(url, headers=headers, data=data, files=files)
31
+
32
+ if response.status_code == 200:
33
+ return response.json()
34
+ return {"status": response.status_code, "detail": response.text}
35
+ except Exception as e:
36
+ return {"error": f"Connection failed: {str(e)}"}
37
+
38
+ # 2. URL Processing Handler
39
+ def process_url_api(url_input, language, target_fmt):
40
+ url = f"{BACKEND_URL}/convert/url"
41
+ headers = get_headers()
42
+ payload = {"url": url_input, "language": language, "to": target_fmt}
43
+
44
+ try:
45
+ with httpx.Client(timeout=60.0) as client:
46
+ response = client.post(url, headers=headers, json=payload)
47
+
48
+ if response.status_code == 200:
49
+ return response.json()
50
+ return {"status": response.status_code, "detail": response.text}
51
+ except Exception as e:
52
+ return {"error": f"Connection failed: {str(e)}"}
53
+
54
+ # 3. Chat Processing Handler
55
+ def process_chat_api(message, history):
56
+ url = f"{BACKEND_URL}/chat"
57
+ headers = get_headers()
58
+ payload = {"message": message}
59
+
60
+ try:
61
+ with httpx.Client(timeout=30.0) as client:
62
+ response = client.post(url, headers=headers, json=payload)
63
+
64
+ if response.status_code == 200:
65
+ res_data = response.json()
66
+ return res_data.get("response", str(res_data))
67
+ return f"Error {response.status_code}: {response.text}"
68
+ except Exception as e:
69
+ return f"Connection error: {str(e)}"
70
+
71
+ # Build the Gradio UI
72
+ with gr.Blocks(title="SOG-X Document Assistant", theme=gr.themes.Soft()) as demo:
73
+ gr.Markdown("# πŸ“„ SOG-X Document & Assistant Suite")
74
+
75
+ with gr.Tab("πŸ“ Upload File"):
76
+ with gr.Row():
77
+ with gr.Column():
78
+ file_input = gr.File(label="Choose Document", file_types=[".pdf", ".docx", ".xlsx", ".pptx", ".txt"])
79
+ file_lang = gr.Textbox(value="en-US", label="Language Code", placeholder="e.g. en-US")
80
+ file_to = gr.Dropdown(choices=["pdf", "docx", "xlsx", "pptx"], value="pdf", label="Target Format")
81
+ file_btn = gr.Button("Convert File", variant="primary")
82
+ with gr.Column():
83
+ file_output = gr.JSON(label="Response Output")
84
+
85
+ file_btn.click(
86
+ process_file_api,
87
+ inputs=[file_input, file_lang, file_to],
88
+ outputs=file_output
89
+ )
90
+
91
+ with gr.Tab("πŸ”— Convert URL"):
92
+ with gr.Row():
93
+ with gr.Column():
94
+ url_input = gr.Textbox(placeholder="https://example.com/document.pdf", label="Document URL")
95
+ url_lang = gr.Textbox(value="en-US", label="Language Code", placeholder="e.g. en-US")
96
+ url_to = gr.Dropdown(choices=["pdf", "docx", "xlsx", "pptx"], value="pdf", label="Target Format")
97
+ url_btn = gr.Button("Convert URL", variant="primary")
98
+ with gr.Column():
99
+ url_output = gr.JSON(label="Response Output")
100
+
101
+ url_btn.click(
102
+ process_url_api,
103
+ inputs=[url_input, url_lang, url_to],
104
+ outputs=url_output
105
+ )
106
+
107
+ with gr.Tab("πŸ’¬ Assistant Chat"):
108
+ gr.ChatInterface(
109
+ fn=process_chat_api,
110
+ textbox=gr.Textbox(placeholder="Ask a question about your documents...", container=False, scale=7),
111
+ title="SOG-X Chat Assistant"
112
+ )
113
+
114
+ if __name__ == "__main__":
115
+ demo.launch()