uumerrr684 commited on
Commit
492796b
·
verified ·
1 Parent(s): d6bd63d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -27
app.py CHANGED
@@ -124,30 +124,24 @@ def get_ai_response(messages, model="openai/gpt-3.5-turbo"):
124
  full_response = ""
125
  buffer = ""
126
 
127
- for chunk in response.iter_content(chunk_size=1024, decode_unicode=True):
128
- if chunk:
129
- buffer += chunk
130
- lines = buffer.split('\n')
131
- buffer = lines[-1] # Keep incomplete line in buffer
132
-
133
- for line in lines[:-1]:
134
- line = line.strip()
135
- if line.startswith('data: '):
136
- line_data = line[6:]
137
- if line_data == '[DONE]':
138
- return
139
- try:
140
- parsed_data = json.loads(line_data)
141
- if 'choices' in parsed_data and len(parsed_data['choices']) > 0:
142
- delta = parsed_data['choices'][0].get('delta', {})
143
- if 'content' in delta:
144
- content = delta['content']
145
- full_response += content
146
- yield full_response
147
- except json.JSONDecodeError:
148
- continue
149
- except Exception as e:
150
- continue
151
 
152
  except requests.exceptions.Timeout:
153
  yield "Request timed out. Please try again with a shorter message or different model."
@@ -177,14 +171,14 @@ with st.sidebar:
177
 
178
  st.divider()
179
 
180
- # Model selection with working models
181
  models = [
182
  ("GPT-3.5 Turbo", "openai/gpt-3.5-turbo"),
183
  ("GPT-4", "openai/gpt-4"),
184
  ("Claude 3 Haiku", "anthropic/claude-3-haiku"),
185
  ("Gemini Pro", "google/gemini-pro"),
186
- ("Llama 3.1 8B (Free)", "meta-llama/llama-3.1-8b-instruct:free"),
187
- ("Llama 3.1 70B (Free)", "meta-llama/llama-3.1-70b-instruct:free"),
188
  ("Llama 3.2 3B (Free)", "meta-llama/llama-3.2-3b-instruct:free"),
189
  ("Qwen 2 7B (Free)", "qwen/qwen-2-7b-instruct:free"),
190
  ("Phi-3 Mini (Free)", "microsoft/phi-3-mini-128k-instruct:free"),
 
124
  full_response = ""
125
  buffer = ""
126
 
127
+ # Using your working streaming logic
128
+ for line in response.iter_lines():
129
+ if line:
130
+ # The server sends lines starting with "data: ..."
131
+ if line.startswith(b"data: "):
132
+ data_str = line[len(b"data: "):].decode("utf-8")
133
+ if data_str.strip() == "[DONE]":
134
+ break
135
+ try:
136
+ data = json.loads(data_str)
137
+ delta = data["choices"][0]["delta"].get("content", "")
138
+ if delta:
139
+ full_response += delta
140
+ yield full_response
141
+ except json.JSONDecodeError:
142
+ continue
143
+ except (KeyError, IndexError):
144
+ continue
 
 
 
 
 
 
145
 
146
  except requests.exceptions.Timeout:
147
  yield "Request timed out. Please try again with a shorter message or different model."
 
171
 
172
  st.divider()
173
 
174
+ # Model selection with working models (based on your working code)
175
  models = [
176
  ("GPT-3.5 Turbo", "openai/gpt-3.5-turbo"),
177
  ("GPT-4", "openai/gpt-4"),
178
  ("Claude 3 Haiku", "anthropic/claude-3-haiku"),
179
  ("Gemini Pro", "google/gemini-pro"),
180
+ ("Llama 3.1 8B", "meta-llama/llama-3.1-8b-instruct"),
181
+ ("Llama 3.1 70B", "meta-llama/llama-3.1-70b-instruct"),
182
  ("Llama 3.2 3B (Free)", "meta-llama/llama-3.2-3b-instruct:free"),
183
  ("Qwen 2 7B (Free)", "qwen/qwen-2-7b-instruct:free"),
184
  ("Phi-3 Mini (Free)", "microsoft/phi-3-mini-128k-instruct:free"),