manaf1234 commited on
Commit
9498ccd
·
verified ·
1 Parent(s): 456d8d2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -0
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+ import io
4
+ import json
5
+ import base64
6
+ from PIL import Image
7
+
8
+ # Initialize the public client
9
+ client = InferenceClient()
10
+
11
+ def universal_inference(model_id, text_input, image_input, audio_input, custom_json):
12
+ if not model_id.strip():
13
+ return "Please enter a valid Hugging Face Model ID.", None, None
14
+
15
+ # 1. Determine Input Payload
16
+ # If custom JSON configuration is provided, use it directly
17
+ if custom_json.strip():
18
+ try:
19
+ payload = json.loads(custom_json)
20
+ except Exception as e:
21
+ return f"Invalid Custom JSON Formatting: {str(e)}", None, None
22
+
23
+ # Otherwise, automatically construct standard structure based on provided inputs
24
+ else:
25
+ payload = {}
26
+ if text_input.strip():
27
+ # Standard formats for LLMs / Text models
28
+ payload["inputs"] = text_input.strip()
29
+
30
+ if image_input is not None:
31
+ # Convert image to base64 for vision/multimodal models
32
+ buffered = io.BytesIO()
33
+ image_input.save(buffered, format="JPEG")
34
+ img_b64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
35
+
36
+ if text_input.strip():
37
+ # VLM / Visual QA format
38
+ payload = {"inputs": {"image": img_b64, "text": text_input.strip()}}
39
+ else:
40
+ # Basic Image-to-Image / Depth-map format
41
+ payload = {"inputs": img_b64}
42
+
43
+ if audio_input is not None:
44
+ # Handle audio file path (read binary)
45
+ with open(audio_input, "rb") as f:
46
+ audio_bytes = f.read()
47
+ audio_b64 = base64.b64encode(audio_bytes).decode('utf-8')
48
+ payload = {"inputs": audio_b64}
49
+
50
+ # 2. Execute Request to Hugging Face Serverless API
51
+ try:
52
+ # We send raw data via POST to allow the API to return whatever the model creates
53
+ response = client.post(json=payload, model=model_id)
54
+ content_type = response.headers.get("content-type", "")
55
+
56
+ # 3. Dynamic Output Routing based on API response type
57
+ # Text Responses (LLM, Translation, Classification, etc.)
58
+ if "text" in content_type or "json" in content_type:
59
+ try:
60
+ parsed_json = response.json()
61
+ return json.dumps(parsed_json, indent=2), None, None
62
+ except:
63
+ return response.text, None, None
64
+
65
+ # Image Responses (Text-to-Image, Inpainting, etc.)
66
+ elif "image" in content_type:
67
+ img = Image.open(io.BytesIO(response.content))
68
+ return "Image successfully generated!", img, None
69
+
70
+ # Audio Responses (TTS, Voice conversion, etc.)
71
+ elif "audio" in content_type or "octet-stream" in content_type:
72
+ # Convert bytes straight to tuple layout for Gradio Audio (data_bytes, format)
73
+ return "Audio successfully generated!", None, response.content
74
+
75
+ else:
76
+ return f"Unknown content return type: {content_type}. Raw data length: {len(response.content)} bytes", None, None
77
+
78
+ except Exception as e:
79
+ return f"Error executing model request:\n{str(e)}\n\n💡 Tip: Verify that the Model ID is typed correctly and is currently active on Hugging Face Serverless API.", None, None
80
+
81
+ # --- GRADIO INTERFACE ---
82
+ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
83
+ gr.Markdown("# 🌐 Universal Zero Chat Any")
84
+ gr.Markdown("Input **any** model from Hugging Face. The app automatically intercepts the output format (Text, Image, or Audio).")
85
+
86
+ with gr.Row():
87
+ with gr.Column(scale=1):
88
+ model_id = gr.Textbox(
89
+ label="🎯 Hugging Face Model ID",
90
+ value="meta-llama/Llama-3.1-8B-Instruct",
91
+ placeholder="e.g., stabilityai/stable-diffusion-3-medium, facebook/mms-tts-eng, etc."
92
+ )
93
+
94
+ gr.Markdown("### Input Fields (Fill out what your target model requires)")
95
+ text_in = gr.Textbox(label="Text Input / Prompt", lines=3)
96
+ image_in = gr.Image(type="pil", label="Image Input (Optional)")
97
+ audio_in = gr.Audio(type="filepath", label="Audio Input (Optional)")
98
+
99
+ with gr.Accordion("⚙️ Advanced: Override with Raw JSON Payload", open=False):
100
+ custom_json = gr.Textbox(
101
+ label="Custom JSON Parameters",
102
+ placeholder='{"inputs": "Your prompt", "parameters": {"temperature": 0.7}}',
103
+ lines=4
104
+ )
105
+
106
+ submit_btn = gr.Button("🚀 Run Inference", variant="primary")
107
+
108
+ with gr.Column(scale=1):
109
+ gr.Markdown("### 📥 Model Output Triggers")
110
+ text_out = gr.Textbox(label="Text Output / Logs", lines=10, interactive=False)
111
+ image_out = gr.Image(label="Generated Image Output")
112
+ audio_out = gr.Audio(label="Generated Audio Output")
113
+
114
+ submit_btn.click(
115
+ universal_inference,
116
+ inputs=[model_id, text_in, image_in, audio_in, custom_json],
117
+ outputs=[text_out, image_out, audio_out]
118
+ )
119
+
120
+ demo.launch()