Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,34 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import json
|
3 |
import time
|
|
|
4 |
|
5 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
QUIZ_DATABASE = {
|
7 |
"oslo": [
|
8 |
{
|
@@ -48,34 +74,66 @@ QUIZ_DATABASE = {
|
|
48 |
]
|
49 |
}
|
50 |
|
51 |
-
def
|
52 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
try:
|
54 |
-
|
55 |
-
tema_lower = tema.lower().strip()
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
-
|
73 |
-
output = f"✅ **Genererte {len(selected)} spørsmål om '{tema}'**\n\n"
|
74 |
-
output += f"🤖 **Modell:** Fallback (forhåndsdefinerte spørsmål)\n"
|
75 |
-
output += f"🌐 **Språk:** {språk}\n"
|
76 |
-
output += f"⏱️ **Tid:** 0.1s\n\n"
|
77 |
|
78 |
-
for i, q in enumerate(
|
79 |
output += f"📝 **Spørsmål {i}:** {q['spørsmål']}\n"
|
80 |
for j, alt in enumerate(q['alternativer']):
|
81 |
marker = "✅" if j == q['korrekt_svar'] else "❌"
|
@@ -85,61 +143,28 @@ def generate_quiz_for_soacti(tema, språk, antall, type_val, vanskelighet):
|
|
85 |
return output
|
86 |
|
87 |
except Exception as e:
|
88 |
-
return f"❌
|
89 |
|
90 |
-
# Gradio interface
|
91 |
with gr.Blocks(title="SoActi AI Quiz API") as demo:
|
92 |
gr.Markdown("# 🧠 SoActi AI Quiz API")
|
93 |
-
gr.Markdown("**Fungerende quiz-generering for SoActi**")
|
94 |
|
95 |
with gr.Row():
|
96 |
with gr.Column():
|
97 |
-
tema_input = gr.Textbox(
|
98 |
-
|
99 |
-
value="Oslo",
|
100 |
-
placeholder="Oslo, Bergen, Norsk historie"
|
101 |
-
)
|
102 |
-
språk_input = gr.Dropdown(
|
103 |
-
choices=["no", "en"],
|
104 |
-
label="Språk",
|
105 |
-
value="no"
|
106 |
-
)
|
107 |
-
antall_input = gr.Slider(
|
108 |
-
minimum=1,
|
109 |
-
maximum=5,
|
110 |
-
step=1,
|
111 |
-
label="Antall spørsmål",
|
112 |
-
value=3
|
113 |
-
)
|
114 |
-
type_input = gr.Dropdown(
|
115 |
-
choices=["sted", "rute"],
|
116 |
-
label="Type",
|
117 |
-
value="sted"
|
118 |
-
)
|
119 |
-
vanskelighet_input = gr.Slider(
|
120 |
-
minimum=1,
|
121 |
-
maximum=5,
|
122 |
-
step=1,
|
123 |
-
label="Vanskelighetsgrad",
|
124 |
-
value=3
|
125 |
-
)
|
126 |
-
|
127 |
generate_btn = gr.Button("🚀 Generer Quiz", variant="primary")
|
128 |
|
129 |
with gr.Column():
|
130 |
-
output = gr.Textbox(
|
131 |
-
label="Generert Quiz",
|
132 |
-
lines=20,
|
133 |
-
placeholder="Klikk 'Generer Quiz' for å starte..."
|
134 |
-
)
|
135 |
|
136 |
generate_btn.click(
|
137 |
-
fn=
|
138 |
-
inputs=[tema_input,
|
139 |
outputs=output
|
140 |
)
|
141 |
-
|
142 |
-
gr.Markdown("## 🔗 API URL for SoActi")
|
143 |
-
gr.Markdown("`https://Soacti-soacti-ai-quiz-api.hf.space`")
|
144 |
|
145 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from fastapi import FastAPI, HTTPException
|
3 |
+
from pydantic import BaseModel
|
4 |
import json
|
5 |
import time
|
6 |
+
from typing import List, Literal
|
7 |
|
8 |
+
# FastAPI app
|
9 |
+
app = FastAPI(title="SoActi AI Quiz API", version="1.0.0")
|
10 |
+
|
11 |
+
# Request/Response models
|
12 |
+
class QuizRequest(BaseModel):
|
13 |
+
tema: str
|
14 |
+
språk: Literal["no", "en"] = "no"
|
15 |
+
antall_spørsmål: int = 5
|
16 |
+
type: Literal["sted", "rute"] = "sted"
|
17 |
+
vanskelighetsgrad: int = 3
|
18 |
+
|
19 |
+
class QuizQuestion(BaseModel):
|
20 |
+
spørsmål: str
|
21 |
+
alternativer: List[str]
|
22 |
+
korrekt_svar: int
|
23 |
+
forklaring: str
|
24 |
+
|
25 |
+
class QuizResponse(BaseModel):
|
26 |
+
success: bool
|
27 |
+
questions: List[QuizQuestion]
|
28 |
+
metadata: dict
|
29 |
+
message: str
|
30 |
+
|
31 |
+
# Hardkodede spørsmål
|
32 |
QUIZ_DATABASE = {
|
33 |
"oslo": [
|
34 |
{
|
|
|
74 |
]
|
75 |
}
|
76 |
|
77 |
+
def get_questions_for_topic(tema: str, antall: int = 5):
|
78 |
+
"""Hent spørsmål for et tema"""
|
79 |
+
tema_lower = tema.lower().strip()
|
80 |
+
|
81 |
+
if tema_lower in QUIZ_DATABASE:
|
82 |
+
questions = QUIZ_DATABASE[tema_lower]
|
83 |
+
elif "oslo" in tema_lower:
|
84 |
+
questions = QUIZ_DATABASE["oslo"]
|
85 |
+
elif "bergen" in tema_lower:
|
86 |
+
questions = QUIZ_DATABASE["bergen"]
|
87 |
+
elif "historie" in tema_lower:
|
88 |
+
questions = QUIZ_DATABASE["norsk historie"]
|
89 |
+
else:
|
90 |
+
questions = QUIZ_DATABASE["oslo"]
|
91 |
+
|
92 |
+
return questions[:antall]
|
93 |
+
|
94 |
+
# API Endpoints
|
95 |
+
@app.get("/")
|
96 |
+
async def root():
|
97 |
+
return {"service": "SoActi AI Quiz API", "status": "running"}
|
98 |
+
|
99 |
+
@app.get("/health")
|
100 |
+
async def health():
|
101 |
+
return {"status": "healthy"}
|
102 |
+
|
103 |
+
@app.post("/generate-quiz", response_model=QuizResponse)
|
104 |
+
async def generate_quiz_api(request: QuizRequest):
|
105 |
+
"""API endpoint - INGEN AUTENTISERING"""
|
106 |
try:
|
107 |
+
questions = get_questions_for_topic(request.tema, request.antall_spørsmål)
|
|
|
108 |
|
109 |
+
quiz_questions = [
|
110 |
+
QuizQuestion(
|
111 |
+
spørsmål=q["spørsmål"],
|
112 |
+
alternativer=q["alternativer"],
|
113 |
+
korrekt_svar=q["korrekt_svar"],
|
114 |
+
forklaring=q["forklaring"]
|
115 |
+
)
|
116 |
+
for q in questions
|
117 |
+
]
|
118 |
+
|
119 |
+
return QuizResponse(
|
120 |
+
success=True,
|
121 |
+
questions=quiz_questions,
|
122 |
+
metadata={"generation_time": 0.1, "model_used": "Database"},
|
123 |
+
message=f"Genererte {len(quiz_questions)} spørsmål"
|
124 |
+
)
|
125 |
|
126 |
+
except Exception as e:
|
127 |
+
raise HTTPException(status_code=500, detail=str(e))
|
128 |
+
|
129 |
+
# Gradio interface
|
130 |
+
def generate_quiz_gradio(tema, språk, antall, type_val, vanskelighet):
|
131 |
+
try:
|
132 |
+
questions = get_questions_for_topic(tema, antall)
|
133 |
|
134 |
+
output = f"✅ Genererte {len(questions)} spørsmål om '{tema}'\n\n"
|
|
|
|
|
|
|
|
|
135 |
|
136 |
+
for i, q in enumerate(questions, 1):
|
137 |
output += f"📝 **Spørsmål {i}:** {q['spørsmål']}\n"
|
138 |
for j, alt in enumerate(q['alternativer']):
|
139 |
marker = "✅" if j == q['korrekt_svar'] else "❌"
|
|
|
143 |
return output
|
144 |
|
145 |
except Exception as e:
|
146 |
+
return f"❌ Feil: {str(e)}"
|
147 |
|
|
|
148 |
with gr.Blocks(title="SoActi AI Quiz API") as demo:
|
149 |
gr.Markdown("# 🧠 SoActi AI Quiz API")
|
|
|
150 |
|
151 |
with gr.Row():
|
152 |
with gr.Column():
|
153 |
+
tema_input = gr.Textbox(label="Tema", value="Oslo")
|
154 |
+
antall_input = gr.Slider(1, 5, 3, label="Antall spørsmål")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
generate_btn = gr.Button("🚀 Generer Quiz", variant="primary")
|
156 |
|
157 |
with gr.Column():
|
158 |
+
output = gr.Textbox(label="Quiz", lines=15)
|
|
|
|
|
|
|
|
|
159 |
|
160 |
generate_btn.click(
|
161 |
+
fn=generate_quiz_gradio,
|
162 |
+
inputs=[tema_input, gr.Dropdown(["no"], value="no"), antall_input, gr.Dropdown(["sted"], value="sted"), gr.Slider(1, 5, 3)],
|
163 |
outputs=output
|
164 |
)
|
|
|
|
|
|
|
165 |
|
166 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
167 |
+
|
168 |
+
if __name__ == "__main__":
|
169 |
+
import uvicorn
|
170 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|