Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,218 +1,24 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
- **Parameters**:
|
23 |
-
- `category` (optional): Filter datasets by a specific category.
|
24 |
-
- `tags` (optional): Filter datasets by tags (comma-separated).
|
25 |
-
- `limit` (optional): Maximum number of datasets to return (default: 10).
|
26 |
-
|
27 |
-
- **Example Request**:
|
28 |
-
```bash
|
29 |
-
curl -X GET "https://api.crustdata.com/datasets?category=finance&tags=economy,stocks&limit=5"
|
30 |
-
```
|
31 |
-
|
32 |
-
- **Example Response**:
|
33 |
-
```json
|
34 |
-
{
|
35 |
-
"datasets": [
|
36 |
-
{
|
37 |
-
"id": "12345",
|
38 |
-
"name": "Global Finance Dataset",
|
39 |
-
"category": "finance",
|
40 |
-
"tags": ["economy", "stocks"]
|
41 |
-
},
|
42 |
-
...
|
43 |
-
]
|
44 |
-
}
|
45 |
-
```
|
46 |
-
|
47 |
-
### 2. **GET /datasets/{id}**
|
48 |
-
- **Description**: Retrieves detailed information about a specific dataset.
|
49 |
-
- **Parameters**:
|
50 |
-
- `id` (required): The unique identifier of the dataset.
|
51 |
-
|
52 |
-
- **Example Request**:
|
53 |
-
```bash
|
54 |
-
curl -X GET "https://api.crustdata.com/datasets/12345"
|
55 |
-
```
|
56 |
-
|
57 |
-
- **Example Response**:
|
58 |
-
```json
|
59 |
-
{
|
60 |
-
"id": "12345",
|
61 |
-
"name": "Global Finance Dataset",
|
62 |
-
"description": "A comprehensive dataset on global financial markets.",
|
63 |
-
"category": "finance",
|
64 |
-
"tags": ["economy", "stocks"],
|
65 |
-
"source": "World Bank"
|
66 |
-
}
|
67 |
-
```
|
68 |
-
|
69 |
-
---
|
70 |
-
|
71 |
-
# Crustdata Discovery and Enrichment API
|
72 |
-
|
73 |
-
## Description
|
74 |
-
The Crustdata Discovery and Enrichment API allows users to enrich their datasets by adding metadata, geolocation information, and other relevant attributes.
|
75 |
-
|
76 |
-
## Key Endpoints
|
77 |
-
|
78 |
-
### 1. **POST /enrich**
|
79 |
-
- **Description**: Enriches input data with additional metadata based on the specified enrichment type.
|
80 |
-
- **Parameters**:
|
81 |
-
- `input_data` (required): A list of data entries to be enriched.
|
82 |
-
- `enrichment_type` (required): The type of enrichment to apply. Supported types:
|
83 |
-
- `geolocation`
|
84 |
-
- `demographics`
|
85 |
-
|
86 |
-
- **Example Request**:
|
87 |
-
```bash
|
88 |
-
curl -X POST "https://api.crustdata.com/enrich" \
|
89 |
-
-H "Content-Type: application/json" \
|
90 |
-
-d '{
|
91 |
-
"input_data": [{"address": "123 Main St, Springfield"}],
|
92 |
-
"enrichment_type": "geolocation"
|
93 |
-
}'
|
94 |
-
```
|
95 |
-
|
96 |
-
- **Example Response**:
|
97 |
-
```json
|
98 |
-
{
|
99 |
-
"enriched_data": [
|
100 |
-
{
|
101 |
-
"address": "123 Main St, Springfield",
|
102 |
-
"latitude": 37.12345,
|
103 |
-
"longitude": -93.12345
|
104 |
-
}
|
105 |
-
]
|
106 |
-
}
|
107 |
-
```
|
108 |
-
|
109 |
-
### 2. **POST /search**
|
110 |
-
- **Description**: Searches for relevant metadata or datasets based on user-provided criteria.
|
111 |
-
- **Parameters**:
|
112 |
-
- `query` (required): The search term or query string.
|
113 |
-
- `filters` (optional): Additional filters to narrow down the search results.
|
114 |
-
|
115 |
-
- **Example Request**:
|
116 |
-
```bash
|
117 |
-
curl -X POST "https://api.crustdata.com/search" \
|
118 |
-
-H "Content-Type: application/json" \
|
119 |
-
-d '{
|
120 |
-
"query": "energy consumption",
|
121 |
-
"filters": {"category": "energy"}
|
122 |
-
}'
|
123 |
-
```
|
124 |
-
|
125 |
-
- **Example Response**:
|
126 |
-
```json
|
127 |
-
{
|
128 |
-
"results": [
|
129 |
-
{
|
130 |
-
"id": "67890",
|
131 |
-
"name": "Energy Consumption Dataset",
|
132 |
-
"category": "energy",
|
133 |
-
"tags": ["consumption", "renewables"]
|
134 |
-
}
|
135 |
-
]
|
136 |
-
}
|
137 |
-
```
|
138 |
-
|
139 |
-
---
|
140 |
-
|
141 |
-
# General Notes
|
142 |
-
- All endpoints require authentication using an API key.
|
143 |
-
- API requests must include the `Authorization` header:
|
144 |
-
```plaintext
|
145 |
-
Authorization: Bearer YOUR_API_KEY
|
146 |
-
```
|
147 |
-
- Response format: JSON
|
148 |
-
- Base URL: `https://api.crustdata.com`
|
149 |
-
"""
|
150 |
-
|
151 |
-
# Split the documentation into smaller chunks
|
152 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
153 |
-
doc_chunks = text_splitter.create_documents([docs])
|
154 |
-
|
155 |
-
# Create embeddings and initialize FAISS vector store
|
156 |
-
embedding_model = "sentence-transformers/all-MiniLM-L6-v2"
|
157 |
-
embeddings = HuggingFaceEmbeddings(model_name=embedding_model)
|
158 |
-
docsearch = FAISS.from_documents(doc_chunks, embeddings)
|
159 |
-
|
160 |
-
|
161 |
-
def retrieve_context(query):
|
162 |
-
"""Retrieve the most relevant context from the knowledge base."""
|
163 |
-
results = docsearch.similarity_search(query, k=2) # Retrieve top 2 most similar chunks
|
164 |
-
context = "\n".join([res.page_content for res in results])
|
165 |
-
return context
|
166 |
-
|
167 |
-
|
168 |
-
def respond(
|
169 |
-
message,
|
170 |
-
history: list[tuple[str, str]],
|
171 |
-
system_message,
|
172 |
-
max_tokens,
|
173 |
-
temperature,
|
174 |
-
top_p,
|
175 |
-
):
|
176 |
-
"""Generate a response using the Hugging Face Inference API."""
|
177 |
-
# Retrieve relevant context from the knowledge base
|
178 |
-
context = retrieve_context(message)
|
179 |
-
prompt = f"{system_message}\n\nContext:\n{context}\n\nUser: {message}\nAssistant:"
|
180 |
-
|
181 |
-
messages = [{"role": "system", "content": system_message}]
|
182 |
-
for val in history:
|
183 |
-
if val[0]:
|
184 |
-
messages.append({"role": "user", "content": val[0]})
|
185 |
-
if val[1]:
|
186 |
-
messages.append({"role": "assistant", "content": val[1]})
|
187 |
-
|
188 |
-
messages.append({"role": "user", "content": prompt})
|
189 |
-
|
190 |
-
response = ""
|
191 |
-
|
192 |
-
for message in client.chat_completion(
|
193 |
-
messages,
|
194 |
-
max_tokens=max_tokens,
|
195 |
-
stream=True,
|
196 |
-
temperature=temperature,
|
197 |
-
top_p=top_p,
|
198 |
-
):
|
199 |
-
token = message.choices[0].delta.content
|
200 |
-
response += token
|
201 |
-
yield response
|
202 |
-
|
203 |
-
|
204 |
-
# Gradio interface
|
205 |
-
demo = gr.ChatInterface(
|
206 |
-
respond,
|
207 |
-
additional_inputs=[
|
208 |
-
gr.Textbox(value="You are a technical assistant for Crustdata APIs.", label="System message"),
|
209 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
210 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
211 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
212 |
-
],
|
213 |
-
title="Crustdata API Chatbot",
|
214 |
-
description="Ask any technical questions about Crustdata’s Dataset and Discovery APIs.",
|
215 |
)
|
216 |
|
217 |
-
|
218 |
-
demo.launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
+
from diffusers import StableDiffusionPipeline
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load the Ghibli LoRA model
|
6 |
+
model_id = "openfree/flux-chatgpt-ghibli-lora"
|
7 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
8 |
+
pipe.to("cpu") # Ensure you have GPU enabled
|
9 |
+
|
10 |
+
def convert_to_ghibli(image):
|
11 |
+
prompt = "A Ghibli-style anime scene, highly detailed, vibrant colors"
|
12 |
+
result = pipe(prompt, image=image).images[0]
|
13 |
+
return result
|
14 |
+
|
15 |
+
# Create Gradio UI
|
16 |
+
iface = gr.Interface(
|
17 |
+
fn=convert_to_ghibli,
|
18 |
+
inputs=gr.Image(type="pil"),
|
19 |
+
outputs="image",
|
20 |
+
title="Ghibli Image Converter",
|
21 |
+
description="Upload an image and convert it to a Studio Ghibli-style artwork using AI."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
)
|
23 |
|
24 |
+
iface.launch()
|
|