hasanalrobasi commited on
Commit
3d0eeb3
·
verified ·
1 Parent(s): 21068d2

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +136 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ from daggr import GradioNode, InferenceNode, FnNode, Graph
3
+ import gradio as gr
4
+ from typing import Dict, Any
5
+ import requests
6
+ import os
7
+
8
+ # Environment variables for API keys
9
+ API_KEYS = {
10
+ "OPENAI": os.getenv("OPENAI_API_KEY"),
11
+ "HUGGINGFACE": os.getenv("HF_API_KEY")
12
+ }
13
+
14
+ # ========== Input Processing Node ==========
15
+ def preprocess_inputs(user_input: str, metadata: Dict[str, Any]) -> Dict[str, Any]:
16
+ """Clean and validate inputs with metadata enrichment"""
17
+ return {
18
+ "cleaned_input": user_input.strip(),
19
+ "timestamp": metadata.get("timestamp"),
20
+ "source": metadata.get("source", "web")
21
+ }
22
+
23
+ input_processor = FnNode(
24
+ fn=preprocess_inputs,
25
+ inputs={
26
+ "user_input": gr.Textbox(label="User Input"),
27
+ "metadata": gr.JSON(label="Metadata")
28
+ },
29
+ outputs={
30
+ "processed_data": gr.JSON(label="Processed Input")
31
+ }
32
+ )
33
+
34
+ # ========== LLM Processing Node ==========
35
+ llm_processor = InferenceNode(
36
+ model="meta-llama/Llama-3-70B-Instruct",
37
+ inputs={
38
+ "prompt": gr.Textbox(label="LLM Prompt"),
39
+ "temperature": gr.Slider(0, 1, value=0.7)
40
+ },
41
+ outputs={
42
+ "response": gr.Textbox(label="LLM Response")
43
+ },
44
+ api_key=API_KEYS["HUGGINGFACE"]
45
+ )
46
+
47
+ # ========== Image Generation Node ==========
48
+ image_generator = GradioNode(
49
+ space_or_url="stabilityai/stable-diffusion-xl-base-1.0",
50
+ api_name="/generate",
51
+ inputs={
52
+ "prompt": gr.Textbox(label="Image Prompt"),
53
+ "negative_prompt": gr.Textbox(label="Negative Prompt"),
54
+ "steps": gr.Slider(10, 50, value=30)
55
+ },
56
+ outputs={
57
+ "image": gr.Image(label="Generated Image")
58
+ }
59
+ )
60
+
61
+ # ========== API Integration Node ==========
62
+ def call_external_api(data: Dict[str, Any]) -> Dict[str, Any]:
63
+ """Generic API caller with error handling"""
64
+ try:
65
+ response = requests.post(
66
+ "https://api.example.com/v1/process",
67
+ json=data,
68
+ headers={"Authorization": f"Bearer {API_KEYS.get('OPENAI')}"},
69
+ timeout=30
70
+ )
71
+ response.raise_for_status()
72
+ return response.json()
73
+ except Exception as e:
74
+ return {"error": str(e)}
75
+
76
+ api_integrator = FnNode(
77
+ fn=call_external_api,
78
+ inputs={
79
+ "api_data": gr.JSON(label="API Payload")
80
+ },
81
+ outputs={
82
+ "api_response": gr.JSON(label="API Results")
83
+ }
84
+ )
85
+
86
+ # ========== Output Formatter Node ==========
87
+ def format_output(llm_response: str, image: Any, api_data: Dict) -> Dict[str, Any]:
88
+ """Create unified output format"""
89
+ return {
90
+ "text_response": llm_response,
91
+ "visual_response": image,
92
+ "api_data": api_data,
93
+ "status": "success"
94
+ }
95
+
96
+ output_formatter = FnNode(
97
+ fn=format_output,
98
+ inputs={
99
+ "llm_response": gr.Textbox(),
100
+ "image": gr.Image(),
101
+ "api_data": gr.JSON()
102
+ },
103
+ outputs={
104
+ "final_output": gr.JSON(label="Final Output")
105
+ }
106
+ )
107
+
108
+ # ========== Create and Connect Workflow ==========
109
+ workflow = Graph(
110
+ name="Global Integration Platform",
111
+ nodes=[
112
+ input_processor,
113
+ llm_processor,
114
+ image_generator,
115
+ api_integrator,
116
+ output_formatter
117
+ ],
118
+ connections=[
119
+ (input_processor.outputs["processed_data"], llm_processor.inputs["prompt"]),
120
+ (input_processor.outputs["processed_data"], image_generator.inputs["prompt"]),
121
+ (input_processor.outputs["processed_data"], api_integrator.inputs["api_data"]),
122
+ (llm_processor.outputs["response"], output_formatter.inputs["llm_response"]),
123
+ (image_generator.outputs["image"], output_formatter.inputs["image"]),
124
+ (api_integrator.outputs["api_response"], output_formatter.inputs["api_data"])
125
+ ]
126
+ )
127
+
128
+ # ========== Launch Application ==========
129
+ if __name__ == "__main__":
130
+ workflow.launch(
131
+ server_name="0.0.0.0",
132
+ server_port=7860,
133
+ share=True,
134
+ auth=("admin", os.getenv("APP_PASSWORD")),
135
+ favicon_path="https://example.com/favicon.ico"
136
+ )
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ daggr>=0.5.4
2
+ gradio>=6.0.2
3
+ requests