Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,55 @@
|
|
1 |
import os
|
2 |
import time
|
3 |
-
import json
|
4 |
-
import httpx
|
5 |
import gradio as gr
|
6 |
from huggingface_hub import InferenceClient
|
7 |
from dotenv import load_dotenv
|
8 |
|
9 |
# Load API keys from .env file
|
10 |
load_dotenv()
|
11 |
-
HF_API_KEY = os.getenv("HF_API_KEY") # Hugging Face API
|
12 |
-
TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY") # Together AI API
|
13 |
|
14 |
-
# Initialize
|
15 |
-
|
16 |
together_client = InferenceClient(provider="together", api_key=TOGETHER_API_KEY)
|
17 |
|
18 |
-
#
|
19 |
-
def
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
43 |
|
44 |
# Function to refine responses using DeepSeek-V3
|
45 |
def refine_response(user_input):
|
46 |
try:
|
47 |
-
# Get responses
|
48 |
-
gemma_response =
|
49 |
-
llama_response =
|
50 |
-
deepseek_response =
|
51 |
-
|
52 |
-
# If any response is missing, return the available ones
|
53 |
-
responses = {
|
54 |
-
"Gemma": gemma_response.strip(),
|
55 |
-
"Llama": llama_response.strip(),
|
56 |
-
"DeepSeek-V3": deepseek_response.strip()
|
57 |
-
}
|
58 |
-
valid_responses = {k: v for k, v in responses.items() if v}
|
59 |
-
|
60 |
-
if len(valid_responses) < 2:
|
61 |
-
return "\n\n".join(f"{k} Response: {v}" for k, v in valid_responses.items())
|
62 |
|
63 |
# Prepare refinement prompt
|
64 |
improvement_prompt = f"""
|
@@ -68,33 +59,17 @@ def refine_response(user_input):
|
|
68 |
Response 2 (Llama 3.3): {llama_response}
|
69 |
Response 3 (DeepSeek-V3): {deepseek_response}
|
70 |
|
71 |
-
Please combine the best elements of all three
|
72 |
"""
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
model="deepseek-ai/DeepSeek-V3",
|
81 |
-
messages=messages,
|
82 |
-
max_tokens=500
|
83 |
-
)
|
84 |
-
|
85 |
-
refined_content = refined_completion.choices[0].message["content"]
|
86 |
-
|
87 |
-
if refined_content.strip():
|
88 |
-
return refined_content
|
89 |
-
else:
|
90 |
-
print("Received empty response from DeepSeek-V3, retrying...")
|
91 |
-
time.sleep(2)
|
92 |
-
|
93 |
-
except Exception as e:
|
94 |
-
print(f"Error on attempt {attempt + 1}: {str(e)}")
|
95 |
-
time.sleep(2)
|
96 |
|
97 |
-
return
|
98 |
|
99 |
except Exception as e:
|
100 |
return f"Error refining response: {str(e)}"
|
@@ -105,8 +80,8 @@ iface = gr.Interface(
|
|
105 |
inputs=gr.Textbox(lines=2, placeholder="Ask me anything..."),
|
106 |
outputs="text",
|
107 |
title="Multi-Model AI Enhancer",
|
108 |
-
description="Get responses from Gemma, Llama
|
109 |
)
|
110 |
|
111 |
# Launch app
|
112 |
-
iface.launch(
|
|
|
1 |
import os
|
2 |
import time
|
|
|
|
|
3 |
import gradio as gr
|
4 |
from huggingface_hub import InferenceClient
|
5 |
from dotenv import load_dotenv
|
6 |
|
7 |
# Load API keys from .env file
|
8 |
load_dotenv()
|
9 |
+
HF_API_KEY = os.getenv("HF_API_KEY") # Hugging Face API Key
|
10 |
+
TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY") # Together AI API Key
|
11 |
|
12 |
+
# Initialize clients
|
13 |
+
gemma_client = InferenceClient(provider="hf-inference", api_key=HF_API_KEY)
|
14 |
together_client = InferenceClient(provider="together", api_key=TOGETHER_API_KEY)
|
15 |
|
16 |
+
# Function to query Hugging Face (Gemma)
|
17 |
+
def query_gemma(user_input):
|
18 |
+
messages = [{"role": "user", "content": user_input}]
|
19 |
+
completion = gemma_client.chat.completions.create(
|
20 |
+
model="google/gemma-2-27b-it",
|
21 |
+
messages=messages,
|
22 |
+
max_tokens=500
|
23 |
+
)
|
24 |
+
return completion.choices[0].message["content"]
|
25 |
+
|
26 |
+
# Function to query Together (Llama)
|
27 |
+
def query_llama(user_input):
|
28 |
+
messages = [{"role": "user", "content": user_input}]
|
29 |
+
completion = together_client.chat.completions.create(
|
30 |
+
model="meta-llama/Llama-3.3-70B-Instruct",
|
31 |
+
messages=messages,
|
32 |
+
max_tokens=500
|
33 |
+
)
|
34 |
+
return completion.choices[0].message["content"]
|
35 |
+
|
36 |
+
# Function to query Together (DeepSeek)
|
37 |
+
def query_deepseek(user_input):
|
38 |
+
messages = [{"role": "user", "content": user_input}]
|
39 |
+
completion = together_client.chat.completions.create(
|
40 |
+
model="deepseek-ai/DeepSeek-V3",
|
41 |
+
messages=messages,
|
42 |
+
max_tokens=500
|
43 |
+
)
|
44 |
+
return completion.choices[0].message["content"]
|
45 |
|
46 |
# Function to refine responses using DeepSeek-V3
|
47 |
def refine_response(user_input):
|
48 |
try:
|
49 |
+
# Get responses
|
50 |
+
gemma_response = query_gemma(user_input)
|
51 |
+
llama_response = query_llama(user_input)
|
52 |
+
deepseek_response = query_deepseek(user_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
# Prepare refinement prompt
|
55 |
improvement_prompt = f"""
|
|
|
59 |
Response 2 (Llama 3.3): {llama_response}
|
60 |
Response 3 (DeepSeek-V3): {deepseek_response}
|
61 |
|
62 |
+
Please combine the best elements of all three and provide an improved answer.
|
63 |
"""
|
64 |
|
65 |
+
messages = [{"role": "user", "content": improvement_prompt}]
|
66 |
+
refined_completion = together_client.chat.completions.create(
|
67 |
+
model="deepseek-ai/DeepSeek-V3",
|
68 |
+
messages=messages,
|
69 |
+
max_tokens=500
|
70 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
return refined_completion.choices[0].message["content"]
|
73 |
|
74 |
except Exception as e:
|
75 |
return f"Error refining response: {str(e)}"
|
|
|
80 |
inputs=gr.Textbox(lines=2, placeholder="Ask me anything..."),
|
81 |
outputs="text",
|
82 |
title="Multi-Model AI Enhancer",
|
83 |
+
description="Get responses from Gemma, Llama, and DeepSeek. Then receive an improved answer."
|
84 |
)
|
85 |
|
86 |
# Launch app
|
87 |
+
iface.launch()
|