Athspi commited on
Commit
59b0fd7
·
verified ·
1 Parent(s): e82d1de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -72
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 for Gemma & Llama
12
- TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY") # Together AI API for DeepSeek-V3
13
 
14
- # Initialize Hugging Face Clients
15
- hf_client = InferenceClient(provider="hf-inference", api_key=HF_API_KEY)
16
  together_client = InferenceClient(provider="together", api_key=TOGETHER_API_KEY)
17
 
18
- # Query Hugging Face Models (Gemma & Llama)
19
- def query_huggingface_model(user_input, model_name):
20
- try:
21
- messages = [{"role": "user", "content": user_input}]
22
- completion = hf_client.chat.completions.create(
23
- model=model_name,
24
- messages=messages,
25
- max_tokens=500
26
- )
27
- return completion.choices[0].message["content"]
28
- except Exception as e:
29
- return f"Error querying {model_name}: {str(e)}"
30
-
31
- # Query DeepSeek-V3 (Together AI via Hugging Face)
32
- def query_deepseek_v3(user_input):
33
- try:
34
- messages = [{"role": "user", "content": user_input}]
35
- completion = together_client.chat.completions.create(
36
- model="deepseek-ai/DeepSeek-V3",
37
- messages=messages,
38
- max_tokens=500
39
- )
40
- return completion.choices[0].message["content"]
41
- except Exception as e:
42
- return f"Error querying DeepSeek-V3: {str(e)}"
 
 
 
 
43
 
44
  # Function to refine responses using DeepSeek-V3
45
  def refine_response(user_input):
46
  try:
47
- # Get responses from all three models
48
- gemma_response = query_huggingface_model(user_input, "google/gemma-2-27b-it")
49
- llama_response = query_huggingface_model(user_input, "meta-llama/Llama-3.3-70B-Instruct")
50
- deepseek_response = query_deepseek_v3(user_input)
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, improve clarity, and provide a final refined answer.
72
  """
73
 
74
- # Retry loop for DeepSeek-V3 refinement
75
- max_retries = 3
76
- for attempt in range(max_retries):
77
- try:
78
- messages = [{"role": "user", "content": improvement_prompt}]
79
- refined_completion = together_client.chat.completions.create(
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 f"Refinement failed. Here’s the best available response:\n\n{max(valid_responses.values(), key=len)}"
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 3.3, and DeepSeek-V3. Then receive an improved final answer."
109
  )
110
 
111
  # Launch app
112
- iface.launch(debug=True)
 
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()