Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files
app.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
st.title("Gemini API Tester")
|
| 6 |
+
|
| 7 |
+
input_text = st.text_area("Type something...")
|
| 8 |
+
|
| 9 |
+
if st.button("Send"):
|
| 10 |
+
try:
|
| 11 |
+
response = requests.post(
|
| 12 |
+
"http://fastapi:8000/generate",
|
| 13 |
+
json={"text": input_text}
|
| 14 |
+
)
|
| 15 |
+
response_data = response.json()
|
| 16 |
+
|
| 17 |
+
if response.status_code == 200:
|
| 18 |
+
st.success(response_data["generated_text"])
|
| 19 |
+
else:
|
| 20 |
+
st.error(f"Error: {response_data.get('detail', 'Unknown error')}")
|
| 21 |
+
except Exception as e:
|
| 22 |
+
st.error(f"Request failed: {e}")
|
main.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#main.py
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
+
|
| 6 |
+
# Cấu hình FastAPI
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# Cấu hình API key cho Gemini
|
| 10 |
+
genai.configure(api_key='AIzaSyBvhkco5aRtkqEyPFFaTKaaDmJkgU12MJU')
|
| 11 |
+
llm_model = genai.GenerativeModel('gemini-1.5-pro')
|
| 12 |
+
|
| 13 |
+
# Khai báo request body để nhận nội dung từ Streamlit
|
| 14 |
+
class InputText(BaseModel):
|
| 15 |
+
text: str
|
| 16 |
+
|
| 17 |
+
# Route xử lý yêu cầu từ Streamlit
|
| 18 |
+
@app.post("/generate")
|
| 19 |
+
async def generate_text(input_text: InputText):
|
| 20 |
+
try:
|
| 21 |
+
# Gọi API Gemini
|
| 22 |
+
response = llm_model.generate_content(input_text.text)
|
| 23 |
+
return {"generated_text": response.text}
|
| 24 |
+
except Exception as e:
|
| 25 |
+
raise HTTPException(status_code=500, detail=f"Error generating content: {e}")
|