Spaces:
Runtime error
Runtime error
import requests | |
import json | |
# url = "http://localhost:8000/chat" # Change to your server address if different | |
# payload = { | |
# "message": "Hello, how are you?", | |
# "messages": [ | |
# { | |
# "role": "system", | |
# "content": [ | |
# { | |
# "type": "text", | |
# "text": "You are a helpful assistant." | |
# } | |
# ] | |
# } | |
# ], | |
# "model": "meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8" | |
# } | |
# headers = { | |
# "Content-Type": "application/json" | |
# } | |
# response = requests.post(url, data=json.dumps(payload), headers=headers, stream=True) | |
# if response.status_code == 200: | |
# print("Streaming response:\n") | |
# try: | |
# for line in response.iter_lines(decode_unicode=True): | |
# if line: | |
# print(line) | |
# except KeyboardInterrupt: | |
# print("\nStopped streaming.") | |
# else: | |
# print("Error:", response.status_code) | |
# print(response.text) | |
url = "http://localhost:8000/generate-topics" | |
payload = { | |
"searchQuery": "Introduction linear integrated circuits" | |
} | |
headers = { | |
"Content-Type": "application/json" | |
} | |
response = requests.post(url, data=json.dumps(payload), headers=headers, stream=True) | |
if response.status_code == 200: | |
print("Streaming response:\n") | |
try: | |
for line in response.iter_lines(decode_unicode=True): | |
if line: | |
if line.startswith("data: "): | |
# Parse the JSON data after "data: " | |
try: | |
json_data = json.loads(line[6:]) # Skip "data: " prefix | |
if "choices" in json_data and json_data["choices"]: | |
if "text" in json_data["choices"][0]: | |
print(json_data["choices"][0]["text"], end="", flush=True) | |
except json.JSONDecodeError: | |
# Handle special case for [DONE] | |
if line[6:] == "[DONE]": | |
break | |
except KeyboardInterrupt: | |
print("\nStopped streaming.") | |
else: | |
print("Error:", response.status_code) | |
print(response.text) |