Athspi commited on
Commit
08f78da
·
verified ·
1 Parent(s): 04c06dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -38
app.py CHANGED
@@ -4,24 +4,18 @@ import json
4
  import httpx
5
  import gradio as gr
6
  from huggingface_hub import InferenceClient
7
- from openai import OpenAI
8
  from dotenv import load_dotenv
9
 
10
  # Load API keys from .env file
11
  load_dotenv()
12
- HF_API_KEY = os.getenv("HF_API_KEY")
13
- OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
14
 
15
  # Initialize Hugging Face Clients
16
  hf_client = InferenceClient(provider="hf-inference", api_key=HF_API_KEY)
 
17
 
18
- # Initialize OpenRouter DeepSeek Client
19
- openrouter_client = OpenAI(
20
- base_url="https://openrouter.ai/api/v1",
21
- api_key=OPENROUTER_API_KEY
22
- )
23
-
24
- # Query Hugging Face Models
25
  def query_huggingface_model(user_input, model_name):
26
  try:
27
  messages = [{"role": "user", "content": user_input}]
@@ -34,30 +28,32 @@ def query_huggingface_model(user_input, model_name):
34
  except Exception as e:
35
  return f"Error querying {model_name}: {str(e)}"
36
 
37
- # Query DeepSeek-R1 (OpenRouter)
38
- def query_deepseek(user_input):
39
  try:
40
- completion = openrouter_client.chat.completions.create(
41
- model="deepseek/deepseek-r1:free",
42
- messages=[{"role": "user", "content": user_input}]
 
 
43
  )
44
- return completion.choices[0].message.content
45
  except Exception as e:
46
- return f"Error querying DeepSeek: {str(e)}"
47
 
48
- # Function to refine responses using DeepSeek
49
  def refine_response(user_input):
50
  try:
51
  # Get responses from all three models
52
  gemma_response = query_huggingface_model(user_input, "google/gemma-2-27b-it")
53
  llama_response = query_huggingface_model(user_input, "meta-llama/Llama-3.3-70B-Instruct")
54
- deepseek_response = query_deepseek(user_input)
55
 
56
  # If any response is missing, return the available ones
57
  responses = {
58
  "Gemma": gemma_response.strip(),
59
  "Llama": llama_response.strip(),
60
- "DeepSeek": deepseek_response.strip()
61
  }
62
  valid_responses = {k: v for k, v in responses.items() if v}
63
 
@@ -70,36 +66,28 @@ def refine_response(user_input):
70
 
71
  Response 1 (Gemma): {gemma_response}
72
  Response 2 (Llama 3.3): {llama_response}
73
- Response 3 (DeepSeek): {deepseek_response}
74
 
75
  Please combine the best elements of all three, improve clarity, and provide a final refined answer.
76
  """
77
 
78
- # Retry loop for OpenRouter API
79
  max_retries = 3
80
  for attempt in range(max_retries):
81
  try:
82
- response = httpx.post(
83
- "https://openrouter.ai/api/v1/chat/completions",
84
- headers={
85
- "Authorization": f"Bearer {OPENROUTER_API_KEY}",
86
- "Content-Type": "application/json"
87
- },
88
- json={
89
- "model": "deepseek/deepseek-r1:free",
90
- "messages": [{"role": "user", "content": improvement_prompt}]
91
- },
92
- timeout=30
93
  )
94
 
95
- print(f"Attempt {attempt + 1}: OpenRouter Response:", response.text)
96
- response_json = response.json()
97
- refined_content = response_json["choices"][0]["message"]["content"]
98
 
99
  if refined_content.strip():
100
  return refined_content
101
  else:
102
- print("Received empty response from DeepSeek, retrying...")
103
  time.sleep(2)
104
 
105
  except Exception as e:
@@ -117,7 +105,7 @@ iface = gr.Interface(
117
  inputs=gr.Textbox(lines=2, placeholder="Ask me anything..."),
118
  outputs="text",
119
  title="Multi-Model AI Enhancer",
120
- description="Get responses from Gemma, Llama 3.3, and DeepSeek. Then receive an improved final answer."
121
  )
122
 
123
  # Launch app
 
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}]
 
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
 
 
66
 
67
  Response 1 (Gemma): {gemma_response}
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:
 
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