Shiva-teja-chary commited on
Commit
281e017
·
verified ·
1 Parent(s): b078141

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +35 -25
main.py CHANGED
@@ -1,37 +1,51 @@
1
  from fastapi import FastAPI
2
  from pydantic import BaseModel
3
- import requests, uvicorn
 
4
 
5
  app = FastAPI()
6
 
7
- # --- Gemini API Setup ---
8
- GEMINI_API_KEY = "AIzaSyApC_u1SScesLRbTfdz5lEcQExDaX-5IwQ"
9
- GEMINI_URL = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={GEMINI_API_KEY}"
 
 
10
 
11
- # --- Input Model ---
 
 
 
 
 
 
12
  class ChatRequest(BaseModel):
13
- query: str # Only query is sent by user
 
 
 
 
14
 
15
- # --- POST /chat ---
16
- @app.post("/chat")
17
  def chat(req: ChatRequest):
18
  try:
19
- # Static instruction (used every time)
20
- static_prompt = (
21
- "You are an assistant. "
22
- "Reply with a short, well-written summary in 2 to 3 sentences. "
23
- "Do not explain unnecessarily. Be clear and concise."
24
- )
25
 
26
  contents = [
27
- {"role": "system", "parts": [{"text": static_prompt}]},
28
- {"role": "user", "parts": [{"text": req.query}]}
29
  ]
30
 
31
- response = requests.post(GEMINI_URL,
32
- headers={"Content-Type": "application/json"},
33
- json={"contents": contents})
34
- response.raise_for_status()
 
 
 
 
 
 
35
 
36
  data = response.json()
37
  reply = (
@@ -40,11 +54,7 @@ def chat(req: ChatRequest):
40
  .get("text", "No reply")
41
  )
42
 
43
- return {"reply": reply}
44
 
45
  except Exception as e:
46
  return {"reply": f"Error: {e}"}
47
-
48
- # --- Run Server ---
49
- if __name__ == "__main__":
50
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  from fastapi import FastAPI
2
  from pydantic import BaseModel
3
+ import requests
4
+ import os
5
 
6
  app = FastAPI()
7
 
8
+ # --- Gemini API key ---
9
+ GEMINI_API_KEY = "AIzaSyApC_u1SScesLRbTfdz5lEcQExDaX-5IwQ" # Replace this with your real key
10
+ GEMINI_URL = (
11
+ "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent"
12
+ )
13
 
14
+ # --- Static prompt ---
15
+ STATIC_INSTRUCTION = (
16
+ "Reply with a short, well-written summary in 2 to 3 sentences. "
17
+ "Be clear and concise. Avoid unnecessary explanations."
18
+ )
19
+
20
+ # --- Input schema ---
21
  class ChatRequest(BaseModel):
22
+ query: str
23
+
24
+ # --- Output schema (optional) ---
25
+ class ChatResponse(BaseModel):
26
+ reply: str
27
 
28
+ # --- Endpoint ---
29
+ @app.post("/chat", response_model=ChatResponse)
30
  def chat(req: ChatRequest):
31
  try:
32
+ # Combine prompt and query as a single user message
33
+ full_prompt = f"{STATIC_INSTRUCTION}\n\nUser question: {req.query}"
 
 
 
 
34
 
35
  contents = [
36
+ {"role": "user", "parts": [{"text": full_prompt}]}
 
37
  ]
38
 
39
+ response = requests.post(
40
+ GEMINI_URL,
41
+ headers={
42
+ "Content-Type": "application/json",
43
+ "x-goog-api-key": GEMINI_API_KEY
44
+ },
45
+ json={"contents": contents}
46
+ )
47
+
48
+ response.raise_for_status() # raise exception if status != 200
49
 
50
  data = response.json()
51
  reply = (
 
54
  .get("text", "No reply")
55
  )
56
 
57
+ return {"reply": reply.strip()}
58
 
59
  except Exception as e:
60
  return {"reply": f"Error: {e}"}