Shreyas094 commited on
Commit
593de4e
·
verified ·
1 Parent(s): 4591c38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -74
app.py CHANGED
@@ -17,14 +17,10 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
17
 
18
  # Environment variables and configurations
19
  huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
20
- ACCOUNT_ID = os.environ.get("CLOUDFARE_ACCOUNT_ID")
21
- API_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")
22
- API_BASE_URL = "https://api.cloudflare.com/client/v4/accounts/a17f03e0f049ccae0c15cdcf3b9737ce/ai/run/"
23
 
24
  MODELS = [
25
  "mistralai/Mistral-7B-Instruct-v0.3",
26
  "mistralai/Mixtral-8x7B-Instruct-v0.1",
27
- "@cf/meta/llama-3.1-8b-instruct",
28
  "mistralai/Mistral-Nemo-Instruct-2407",
29
  "meta-llama/Meta-Llama-3.1-8B-Instruct",
30
  "meta-llama/Meta-Llama-3.1-70B-Instruct"
@@ -112,76 +108,21 @@ def get_response_with_search(query, model, num_calls=3, temperature=0.2):
112
  Write a detailed and complete research document that fulfills the following user request: '{query}'
113
  After writing the document, please provide a list of sources used in your response."""
114
 
115
- if model == "@cf/meta/llama-3.1-8b-instruct":
116
- # Use Cloudflare API
117
- for response in get_response_from_cloudflare(prompt="", context=context, query=query, num_calls=num_calls, temperature=temperature):
118
- yield response, "" # Yield streaming response without sources
119
- else:
120
- # Use Hugging Face API
121
- client = InferenceClient(model, token=huggingface_token)
122
-
123
- main_content = ""
124
- for i in range(num_calls):
125
- for message in client.chat_completion(
126
- messages=[{"role": "user", "content": prompt}],
127
- max_tokens=10000,
128
- temperature=temperature,
129
- stream=True,
130
- ):
131
- if message.choices and message.choices[0].delta and message.choices[0].delta.content:
132
- chunk = message.choices[0].delta.content
133
- main_content += chunk
134
- yield main_content, "" # Yield partial main content without sources
135
-
136
- def get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2):
137
- headers = {
138
- "Authorization": f"Bearer {API_TOKEN}",
139
- "Content-Type": "application/json"
140
- }
141
- model = "@cf/meta/llama-3.1-8b-instruct"
142
-
143
- instruction = f"""Using the following context:
144
- {context}
145
- Write a detailed and complete research document that fulfills the following user request: '{query}'
146
- After writing the document, please provide a list of sources used in your response."""
147
-
148
- inputs = [
149
- {"role": "system", "content": instruction},
150
- {"role": "user", "content": query}
151
- ]
152
-
153
- payload = {
154
- "messages": inputs,
155
- "stream": True,
156
- "temperature": temperature,
157
- "max_tokens": 32000
158
- }
159
-
160
- full_response = ""
161
- for i in range(num_calls):
162
- try:
163
- with requests.post(f"{API_BASE_URL}{model}", headers=headers, json=payload, stream=True) as response:
164
- if response.status_code == 200:
165
- for line in response.iter_lines():
166
- if line:
167
- try:
168
- json_response = json.loads(line.decode('utf-8').split('data: ')[1])
169
- if 'response' in json_response:
170
- chunk = json_response['response']
171
- full_response += chunk
172
- yield full_response
173
- except (json.JSONDecodeError, IndexError) as e:
174
- logging.error(f"Error parsing streaming response: {str(e)}")
175
- continue
176
- else:
177
- logging.error(f"HTTP Error: {response.status_code}, Response: {response.text}")
178
- yield f"I apologize, but I encountered an HTTP error: {response.status_code}. Please try again later."
179
- except Exception as e:
180
- logging.error(f"Error in generating response from Cloudflare: {str(e)}")
181
- yield f"I apologize, but an error occurred: {str(e)}. Please try again later."
182
 
183
- if not full_response:
184
- yield "I apologize, but I couldn't generate a response at this time. Please try again later."
 
 
 
 
 
 
 
 
 
 
185
 
186
  def vote(data: gr.LikeData):
187
  if data.liked:
@@ -205,7 +146,7 @@ def initial_conversation():
205
  demo = gr.ChatInterface(
206
  respond,
207
  additional_inputs=[
208
- gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
209
  gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
210
  gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
211
  ],
 
17
 
18
  # Environment variables and configurations
19
  huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
 
 
 
20
 
21
  MODELS = [
22
  "mistralai/Mistral-7B-Instruct-v0.3",
23
  "mistralai/Mixtral-8x7B-Instruct-v0.1",
 
24
  "mistralai/Mistral-Nemo-Instruct-2407",
25
  "meta-llama/Meta-Llama-3.1-8B-Instruct",
26
  "meta-llama/Meta-Llama-3.1-70B-Instruct"
 
108
  Write a detailed and complete research document that fulfills the following user request: '{query}'
109
  After writing the document, please provide a list of sources used in your response."""
110
 
111
+ # Use Hugging Face API
112
+ client = InferenceClient(model, token=huggingface_token)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
 
114
+ main_content = ""
115
+ for i in range(num_calls):
116
+ for message in client.chat_completion(
117
+ messages=[{"role": "user", "content": prompt}],
118
+ max_tokens=10000,
119
+ temperature=temperature,
120
+ stream=True,
121
+ ):
122
+ if message.choices and message.choices[0].delta and message.choices[0].delta.content:
123
+ chunk = message.choices[0].delta.content
124
+ main_content += chunk
125
+ yield main_content, "" # Yield partial main content without sources
126
 
127
  def vote(data: gr.LikeData):
128
  if data.liked:
 
146
  demo = gr.ChatInterface(
147
  respond,
148
  additional_inputs=[
149
+ gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[2]),
150
  gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
151
  gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
152
  ],