ParthSadaria commited on
Commit
1ba1f47
·
verified ·
1 Parent(s): aeb51aa

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +31 -5
main.py CHANGED
@@ -40,6 +40,16 @@ async def favicon():
40
  return FileResponse(favicon_path, media_type="image/x-icon")
41
 
42
 
 
 
 
 
 
 
 
 
 
 
43
  def generate_search(query: str, stream: bool = True) -> str:
44
  headers = {"User-Agent": ""}
45
  prompt = [
@@ -64,14 +74,30 @@ def generate_search(query: str, stream: bool = True) -> str:
64
  try:
65
  json_modified_value = json.loads(value[6:])
66
  content = json_modified_value.get("choices", [{}])[0].get("delta", {}).get("content", "")
67
- if stream:
68
- yield f"data: {json.dumps(json_modified_value)}\n\n"
69
- streaming_text += content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  except json.JSONDecodeError:
71
  continue
72
 
73
  if not stream:
74
- # For non-streaming, just yield the text content
75
  yield streaming_text
76
 
77
  @app.get("/searchgpt")
@@ -88,7 +114,7 @@ async def search_gpt(q: str, stream: Optional[bool] = False):
88
  # For non-streaming, collect the text and return as JSON response
89
  response_text = "".join([chunk for chunk in generate_search(q, stream=False)])
90
  return JSONResponse(content={"response": response_text})
91
-
92
  @app.get("/", response_class=HTMLResponse)
93
  async def root():
94
  # Open and read the content of index.html (in the same folder as the app)
 
40
  return FileResponse(favicon_path, media_type="image/x-icon")
41
 
42
 
43
+ import json
44
+ import requests
45
+ from fastapi import FastAPI, HTTPException
46
+ from fastapi.responses import StreamingResponse, JSONResponse
47
+ from typing import Optional
48
+
49
+ app = FastAPI()
50
+
51
+ secret_api_endpoint_3 = "your_secret_endpoint_here" # Replace with your actual endpoint
52
+
53
  def generate_search(query: str, stream: bool = True) -> str:
54
  headers = {"User-Agent": ""}
55
  prompt = [
 
74
  try:
75
  json_modified_value = json.loads(value[6:])
76
  content = json_modified_value.get("choices", [{}])[0].get("delta", {}).get("content", "")
77
+
78
+ if content.strip(): # Only process non-empty content
79
+ cleaned_response = {
80
+ "created": json_modified_value.get("created"),
81
+ "id": json_modified_value.get("id"),
82
+ "model": "searchgpt",
83
+ "object": "chat.completion",
84
+ "choices": [
85
+ {
86
+ "message": {
87
+ "content": content
88
+ }
89
+ }
90
+ ]
91
+ }
92
+
93
+ if stream:
94
+ yield f"data: {json.dumps(cleaned_response)}\n\n"
95
+
96
+ streaming_text += content
97
  except json.JSONDecodeError:
98
  continue
99
 
100
  if not stream:
 
101
  yield streaming_text
102
 
103
  @app.get("/searchgpt")
 
114
  # For non-streaming, collect the text and return as JSON response
115
  response_text = "".join([chunk for chunk in generate_search(q, stream=False)])
116
  return JSONResponse(content={"response": response_text})
117
+
118
  @app.get("/", response_class=HTMLResponse)
119
  async def root():
120
  # Open and read the content of index.html (in the same folder as the app)