ogegadavis254 commited on
Commit
8092b5a
·
verified ·
1 Parent(s): 71ec4a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -6
app.py CHANGED
@@ -29,10 +29,7 @@ def call_ai_model(all_message):
29
  response = requests.post(url, json=payload, headers=headers, stream=True)
30
  response.raise_for_status() # Ensure HTTP request was successful
31
 
32
- # Debugging line to print raw response
33
- for line in response.iter_lines():
34
- if line:
35
- yield line.decode('utf-8')
36
 
37
  # Streamlit app layout
38
  st.title("Climate Change Impact on Sports Using AI")
@@ -49,8 +46,25 @@ if st.button("Generate Prediction"):
49
 
50
  try:
51
  with st.spinner("Generating response..."):
52
- response_lines = call_ai_model(all_message)
53
- generated_text = "".join(response_lines)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  st.success("Response generated successfully!")
55
  st.write(generated_text)
56
  except ValueError as ve:
 
29
  response = requests.post(url, json=payload, headers=headers, stream=True)
30
  response.raise_for_status() # Ensure HTTP request was successful
31
 
32
+ return response
 
 
 
33
 
34
  # Streamlit app layout
35
  st.title("Climate Change Impact on Sports Using AI")
 
46
 
47
  try:
48
  with st.spinner("Generating response..."):
49
+ response = call_ai_model(all_message)
50
+
51
+ # Handle the streaming response and concatenate chunks
52
+ generated_text = ""
53
+ for line in response.iter_lines():
54
+ if line:
55
+ line_content = line.decode('utf-8')
56
+ if line_content.startswith("data: "):
57
+ line_content = line_content[6:] # Strip "data: " prefix
58
+ try:
59
+ json_data = json.loads(line_content)
60
+ if "choices" in json_data:
61
+ delta = json_data["choices"][0]["delta"]
62
+ if "content" in delta:
63
+ generated_text += delta["content"]
64
+ st.write(generated_text) # Update the displayed text incrementally
65
+ except json.JSONDecodeError:
66
+ continue
67
+
68
  st.success("Response generated successfully!")
69
  st.write(generated_text)
70
  except ValueError as ve: